{"id":1464,"date":"2011-02-21T10:09:02","date_gmt":"2011-02-21T09:09:02","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=1464"},"modified":"2011-02-21T10:09:02","modified_gmt":"2011-02-21T09:09:02","slug":"c-reference-parameters-the-downside","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2011\/02\/21\/c-reference-parameters-the-downside\/","title":{"rendered":"C++ reference parameters &#8211; the downside"},"content":{"rendered":"<p>Something that I have discovered over the years is a great pleasure. When I am giving information &#8211; presenting, teaching, writing an article or a blog &#8211; it is not necessarily a one-way process. I often receive useful and interesting information back. I have commented that I learn as much from delivering a class as I might from attending one.<\/p>\n<p>I was recently talking about C++ for embedded at a conference and considering some of the features that I felt resulted in better, clearer and more bug-free code. One of these was the option to pass parameters by reference. Then someone explained a drawback of this feature &#8230;<!--more--><\/p>\n<p>Pointers are a very powerful feature of the C language and largely explain its wide usage for embedded programming. But they can also be confusing and error prone. For example, consider this code:<\/p>\n<pre>int *p;\np = (int *)0x8000;\np += 1;   \/\/ this could equally be p=p+1 or p++<\/pre>\n<p>What is the final value of <strong>p<\/strong>?<\/p>\n<p>Assuming the compiler in use regards integers as 32 bits, the answer is <strong>0x8004<\/strong>.<\/p>\n<p>Although, to the experienced programmer, this may be crystal clear, it is far from obvious to someone coming to the language for the first time.<\/p>\n<p>Of course, C++ has pointers too and this example behaves in the same way, but C++ has some opportunities to avoid the use of pointers and, hence, write clearer code. And one of those is the option to pass parameters by reference.<\/p>\n<p>This code might be written in C, for example:<\/p>\n<pre>void swap(int *a, int *b)\n{\n   int temp = *a;\n   *a = *b;\n   *b = temp;\n}<\/pre>\n<p>The parameters are pointers to data in the calling function and this code is littered with pointer operations, every one of which is a potential error. So, in C++ we can pass the parameters by reference, thus:<\/p>\n<pre>void swap(int&amp; a, int&amp; b)\n{\n   int temp = a;\n   a = b;\n   b = temp;\n}<\/pre>\n<p>Now, the calling function does not need to provide [explicit] pointers and the pointer operations are eliminated in the <strong>swap()<\/strong> function, rendering it more readable and less likely to contain a bug.<\/p>\n<p>This is all fine, but the problem that was pointed out to me was with a call to <strong>swap()<\/strong>. In C, the call would look like this:<\/p>\n<pre>swap(&amp;x, &amp;y);<\/pre>\n<p>It is 100% clear that pointers have been passed and, hence, the possibility that the values of <strong>x<\/strong> and <strong>y<\/strong> might be changed by the call to <strong>swap()<\/strong> is quite clear. However, in C++, if reference parameters were used, the call would be written like this:<\/p>\n<pre>swap(x, y);<\/pre>\n<p>And there is no clear indication, without resort to the function prototype [which would, of course, be readily on hand if you are using a good IDE], that <strong>x<\/strong> and <strong>y<\/strong> could get changed.<\/p>\n<p>I guess it is a case of &#8220;what you win on the swings, you lose on the roundabouts&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something that I have discovered over the years is a great pleasure. When I am giving information &#8211; presenting, teaching,&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-1464","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=1464"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1464\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=1464"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=1464"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=1464"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}