{"id":424,"date":"2008-01-30T18:23:47","date_gmt":"2008-01-31T00:23:47","guid":{"rendered":"http:\/\/blog.codedread.com\/archives\/2008\/01\/30\/on-the-merits-of-const\/"},"modified":"2008-01-30T18:23:47","modified_gmt":"2008-01-31T00:23:47","slug":"on-the-merits-of-const","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2008\/01\/30\/on-the-merits-of-const\/","title":{"rendered":"On The Merits Of Const&#8230;"},"content":{"rendered":"<p>A short post about using const in C++.  <!--more--><\/p>\n<p>Consider the following 3 for-loops in C\/C++:<\/p>\n<div class=\"code\">\n<p>\/* Loop 1: *\/<\/p>\n<p>for(int i = 0; i &#60; getSomeValue(); ++i) { ...lots of stuff in here... }<\/p>\n<p>\/* Loop 2: *\/<\/p>\n<p>int n = getSomeValue();<\/p>\n<p>for(int i = 0; i &#60; n; ++i) { ...lots of stuff in here... }<\/p>\n<p>\/* Loop 3: *\/<\/p>\n<p>const int n = getSomeValue();<\/p>\n<p>for(int i = 0; i &#60; n; ++i) { ...lots of stuff in here... }<\/p>\n<\/div>\n<p>Best Practice:  If the value of 'n' is not supposed to change throughout the lifetime of the loop, then I think everyone can agree that using const in Loop 3 is a best practice.  This helps ensure that some other coder doesn't muck about with the loop's end condition inside the loop (since it will be flagged with a compiler error).<\/p>\n<p>Optimization: It makes sense that, in general, Loop 2 would execute faster than Loop 1.  But I was curious if a compiler could optimize Loop 3 to make it run faster than Loop 2?  It's been awhile since I looked at any assembly output, and my compiler theory has always been rather weak, so I thought I'd throw the question out there in case anyone has a clue...<\/p>\n<div class=\"ads\"><object type=\"text\/html\" width=\"468\" height=\"60\" data=\"http:\/\/www.codedread.com\/gads.php\"><\/object><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A short post about using const in C++.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,25,11],"tags":[],"class_list":["post-424","post","type-post","status-publish","format-standard","hentry","category-c","category-software","category-technology"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":0,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}