{"id":3392,"date":"2019-11-08T12:51:18","date_gmt":"2019-11-08T12:51:18","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=3392"},"modified":"2024-02-16T10:20:13","modified_gmt":"2024-02-16T10:20:13","slug":"common-reasons-for-program-crashes-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=3392","title":{"rendered":"Common reasons for program crashes (C++)"},"content":{"rendered":"<p><strong>1. Illegal memory access<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: Deref. uninit. memory; notranslate\" title=\"Deref. uninit. memory\">\r\nint main()\r\n{\r\n  int* ptr;\r\n  int num = ptr[0];\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: Double free; notranslate\" title=\"Double free\">\r\nint main()\r\n{\r\n  int* ptr = new int(42);\r\n  delete ptr;\r\n  delete ptr;\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: Buffer overflow; notranslate\" title=\"Buffer overflow\">\r\nint main()\r\n{\r\n  int ary[5];\r\n  ary[5] = 42;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>2. Allocating to much memory<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: Heap overflow; notranslate\" title=\"Heap overflow\">\r\nint main()\r\n{\r\n  for(;;)\r\n  {\r\n    int* ptr = new int[1000000000];\r\n  }\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: Stack overflow; notranslate\" title=\"Stack overflow\">\r\nint main()\r\n{\r\n  int ary[1000000000];\r\n  ary[0] = 42;\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: Infinite recursion; notranslate\" title=\"Infinite recursion\">\r\nvoid fct()\r\n{\r\n  fct();\r\n}\r\n\r\nint main()\r\n{\r\n  fct();\r\n}\r\n<\/pre>\n<hr>\n<p><strong>3. Unsolved error handling<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: Assertion; notranslate\" title=\"Assertion\">\r\n#include &lt;assert.h&gt; \r\n\r\nint main()\r\n{\r\n  int* ptr = nullptr;\r\n  assert(ptr);\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: Exception (uncaught); notranslate\" title=\"Exception (uncaught)\">\r\n#include &lt;exception&gt;\r\n\r\nint main()\r\n{\r\n  throw std::exception();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Illegal memory access 2. Allocating to much memory 3. Unsolved error handling<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3392"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3392"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3392\/revisions"}],"predecessor-version":[{"id":16733,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3392\/revisions\/16733"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}