{"id":259,"date":"2018-11-18T23:07:56","date_gmt":"2018-11-18T22:07:56","guid":{"rendered":"http:\/\/xpam.pl\/blog\/?p=259"},"modified":"2024-04-03T00:11:29","modified_gmt":"2024-04-02T22:11:29","slug":"that-moment-when-you-need-to-look-up-definition-of-c-for-loop","status":"publish","type":"post","link":"https:\/\/xpam.pl\/blog\/?p=259","title":{"rendered":"That moment when you need to look up definition of C++ for loop"},"content":{"rendered":"<p>I was getting a segfault on an old piece of code which I maintain. The culprit was pinpointed to this:<\/p>\n<div class=\"de1\">\n<pre><code class=\"language-cpp\">bool found = false;\nvector&lt;string&gt; :: iterator i;\nfor (i = v.begin(); !found &amp;&amp; i != v.end(); ++i) {\n    if (name == *i) {\n        found = true;\n    }\n}\nif (found) {\n   v.erase( i ); \/\/ &lt;-- segfault here\n}<\/code><\/pre>\n<p>I went through this piece if code at least 10 times without noticing the problem. The snippet is simple enough.. when match is found, set <em>found<\/em> to true and that breaks the loop since loop condition now evaluates to false. The iterator remains at the position of matched element.<\/p>\n<p><strong>WRONG<\/strong>.<\/p>\n<p>What we are actually getting is iterator+1.<\/p>\n<p><a href=\"http:\/\/xpam.pl\/blog\/wp-content\/uploads\/2018\/11\/cpp_for_loop.jpg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-260 size-full\" src=\"http:\/\/xpam.pl\/blog\/wp-content\/uploads\/2018\/11\/cpp_for_loop.jpg.png\" alt=\"\" width=\"359\" height=\"504\" srcset=\"https:\/\/xpam.pl\/blog\/wp-content\/uploads\/2018\/11\/cpp_for_loop.jpg.png 359w, https:\/\/xpam.pl\/blog\/wp-content\/uploads\/2018\/11\/cpp_for_loop.jpg-214x300.png 214w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/a><\/p>\n<p>What we don't see directly from the code is that increment happens before the condition is evaluated for the next loop, giving us iterator+1 which causes a segfault if match is found on last element.<\/p>\n<\/div>\n<div class=\"wp-post-signature\">\r\n<br \/>\r\n<br \/>\r\n<img src='https:\/\/xpam.pl\/aaaaff.png' title='Moonie' \/> Cen<br \/>\r\n<a href='https:\/\/github.com\/cen1'>GitHub<\/a><br \/>\r\n<a href='https:\/\/eurobattle.net'>Eurobattle.net<\/a><br \/>\r\n<a href='https:\/\/lagabuse.com'>Lagabuse.com<\/a><br \/>\r\n<a href='https:\/\/bnetdocs.org'>Bnetdocs<\/a><br \/>\r\n<\/div>\r\n","protected":false},"excerpt":{"rendered":"<p>I was getting a segfault on an old piece of code which I maintain. The culprit was pinpointed to this: bool found = false; vector&lt;string&gt; :: iterator i; for (i = v.begin(); !found &amp;&amp; i != v.end(); ++i) { if (name == *i) { found = true; } } if (found) { v.erase( i ); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=\/wp\/v2\/posts\/259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=259"}],"version-history":[{"count":4,"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=\/wp\/v2\/posts\/259\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xpam.pl\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}