{"id":1855,"date":"2018-11-22T14:59:23","date_gmt":"2018-11-22T14:59:23","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=1855"},"modified":"2024-02-16T10:26:15","modified_gmt":"2024-02-16T10:26:15","slug":"common-mistakes-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=1855","title":{"rendered":"Common mistakes (C++)"},"content":{"rendered":"<p>The following list shows a few common mistakes you will find in code you will have to review before its check-in.<\/p>\n<ol>\n<li>Calling a method of an object behind a raw pointer without checking on nullptr before<\/li>\n<ul>\n<li><u>Details:<\/u> If you aren&#8217;t setting the raw pointer manually to nullptr after deleting it you won&#8217;t find it out. That&#8217;s the reason why bigger projects either use smart pointer instead of raw pointer or have a lifetime tracker implementation.<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nstruct Foo {\r\n  int i = 0;\r\n  void foo(){ i++; }\r\n};\r\n\r\nint main() {\r\n  Foo* f;\r\n  f-&gt;foo(); \/\/ boom\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Accessing a container without checking for its size or setting up a catch in case of an exception<\/li>\n<ul>\n<li><u>Details:<\/u> Not all container throw for every not possible access an exception (e.g. std::vector::front).<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nint main() {\r\n  std::vector&lt;int&gt; v;\r\n  int d = v.front(); \/\/ boom\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Forgotten virtual destructor in case object gets destructed via base class<\/li>\n<ul>\n<li><u>Details:<\/u> Just the destructor of the base class will be executed, which leads to a memory leak, cause all other member variables of this object won&#8217;t get deleted.<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nstruct Foo {\r\n  int i = 0;\r\n};\r\n\r\nstruct Bar : public Foo {\r\n  int d = 0;\r\n};\r\n \r\nint main() {\r\n  Foo *f = new Bar();\r\n  delete f; \/\/ just calls destructor of Foo\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Forgotten synchronization for multithreaded access on same resources<\/li>\n<ul>\n<li><u>Details:<\/u> This will lead to a race condition.<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nstruct Foo {\r\n  int i = 0;\r\n  void foo() {\r\n    for(int d=0; d&lt;5000000; d++) { i++; }\r\n  }\r\n};\r\n \r\nint main() {\r\n  Foo f;\r\n  std::thread t1(&amp;Foo::foo, &amp;f), t2(&amp;Foo::foo, &amp;f);\r\n  t1.join(); t2.join();\r\n  \/\/ f.i will be most likely &lt; 10000000\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Forgotten to release a block mechanism for multithreaded execution<\/li>\n<ul>\n<li><u>Details:<\/u> This will lead to a deadlock. Prefer the usage of a std::lock_guard over std::mutex::lock\/unlock to prevent this.<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nstruct Foo {\r\n  std::mutex m;\r\n  void foo() { m.lock(); } \/\/ the 2nd thread will be blocked\r\n};\r\n \r\nint main() {\r\n  Foo f;\r\n  std::thread t1(&amp;Foo::foo, &amp;f), t2(&amp;Foo::foo, &amp;f);\r\n  t1.join(); t2.join();\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Passing a ref\/raw_ptr of a local variable to a function, which get executed by another thread<\/li>\n<ul>\n<li><u>Details:<\/u> This leads possibly to an access on an already destructed variable.<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nvoid foo(std::string* s) {\r\n  (*s) += &quot; they lived happily ever after&quot;;\r\n}\r\n\r\nint main() {\r\n  std::string s = &quot;Once upon a time ...&quot;;\r\n  std::thread t(foo, &amp;s); \/\/ boom\r\n  \/\/ t.join(); \/\/ --&gt; no boom\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Passing a copy of a big local variable to a function, which get executed by the same thread<\/li>\n<ul>\n<li><u>Details:<\/u> This problem doesn&#8217;t occur for small objects like int cause the size of an pointer address can be even greater.<\/li>\n<li><u>Example:<\/u>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\nvoid foo(std::string s) {}\r\n\r\nint main() {\r\n  std::string s;\r\n  s.resize(5000000000);\r\n  std::fill_n(s.begin(), s.size()\/sizeof(char), &#039;0&#039;);\r\n  foo(s); \/\/ slow\r\n  return 0;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<li>Forgotten code documentation (e.g. Doxygen, <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=1084\">Follow the link<\/a>)<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>The following list shows a few common mistakes you will find in code you will have to review before its<\/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":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1855"}],"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=1855"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1855\/revisions"}],"predecessor-version":[{"id":16756,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1855\/revisions\/16756"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1855"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}