{"id":9044,"date":"2019-07-15T08:40:03","date_gmt":"2019-07-15T07:40:03","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=9044"},"modified":"2019-07-15T08:40:03","modified_gmt":"2019-07-15T07:40:03","slug":"some-more-embedded-software-programming-tips-2","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2019\/07\/15\/some-more-embedded-software-programming-tips-2\/","title":{"rendered":"Some more embedded software programming tips"},"content":{"rendered":"<p>Another in my occasional series of tips for embedded software developers. Sometimes my tips are just personal taste; on other occasions they are more like hard facts. But, of course, someone might disagree \u2026<!--more--><\/p>\n<p>Three tips today:<\/p>\n<h3>Do not write while(something); &#8211; put the semicolon on the next line<\/h3>\n<p>Sometimes it is necessary to have a loop that just waits for something to occur and this most likely includes an empty statement. Although this is fine, some effort should be made to ensure that the code is readable and clear. Writing it like this is not very clear:<\/p>\n<pre><strong>while (device_ready() == 0);<\/strong><\/pre>\n<p>However, it take very little to make it better:<\/p>\n<pre><strong>while (device_ready() == 0)   \/\/ wait for response from interface<\/strong>\n<strong>   ;<\/strong><\/pre>\n<p>Some care is still needed, as this code might unintentionally become an infinite loop &#8211; if the device never responded. Whenever possible, include a timeout:<\/p>\n<pre><strong>error_log = 0;<\/strong>\n<strong>timeout(100);                 \/\/ expect a response in 100ms<\/strong>\n<strong>while (device_ready() == 0)   \/\/ wait for response from interface<\/strong>\n<strong>   if (test_timeout())<\/strong>\n<strong>   {<\/strong>\n<strong>      error_log = DEVICE_TIMEOUT;<\/strong>\n<strong>      break;<\/strong>\n<strong>   }<\/strong>\n\n<strong>if (error_log != 0)<\/strong>\n<strong>   ...<\/strong><\/pre>\n<h3>In a real time system design, the word \u201cdynamic\u201d should ring alarm bells<\/h3>\n<p>In general English usage, the word \u201cdynamic\u201d has almost totally positive connotations. I would be delighted to have this adjective applied to me &#8211; though it is quite unlikely! In the context of software, the meaning is more specific: dynamic things are created and destroyed, as required. For example, dynamic memory can be allocated when it is required and de-allocated when the need has passed. This is very convenient, but comes at a cost. The management of dynamic memory tends to be non-deterministic &#8211; i.e. it does not behave in the predictable way that is required to build a real-time system. Additionally, failure modes can be problematic. A while ago, I presented a webinar on this topic and the <a href=\"https:\/\/www.mentor.com\/embedded-software\/multimedia\/dynamic-memory-allocation---fragmentation-in-c---c--\" target=\"_blank\" rel=\"noopener noreferrer\">recording<\/a> is still available.<\/p>\n<p>The best solution is to use an real-time operating system, like our <a href=\"https:\/\/www.mentor.com\/embedded-software\/nucleus\/\" target=\"_blank\" rel=\"noopener noreferrer\">Nucleus RTOS<\/a> product, that provides memory allocation facilities that are compatible with the needs of a real-time application.<\/p>\n<h3>In C++, overloaded functions have zero impact on code size or performance<\/h3>\n<p>At first sight, being able to have two different functions with the same name sounds confusing, but it can actually be used to write clear code. C++ offers two facilities along these lines: templates and overloaded functions. Templates are a newer language facility and, whilst they can be useful, there are some challenges with their use. Overloaded functions are simpler and have been in the C++ language since the start. Indeed the concept of an overloaded function goes back to the Fortran language in the mid-1960s.<\/p>\n<p>If you need to perform similar operations on different types of data, overloaded functions are a very clear way to code the functionality. All you need to do is ensure that each function has a different combination of parameter types\/numbers. So, you can write code like this:<\/p>\n<pre><strong>int max(int, int);<\/strong>\n<strong>int max(int, int, int);<\/strong>\n<strong>float max(float, float);<\/strong><\/pre>\n<p>Each <strong>max()<\/strong> function is different and the compiler will use the appropriate one according to the provided parameters. This works because C++ \u201cmangles\u201d function names to include parameter number and type information. There is absolutely no \u201ccost\u201d for this facility &#8211; no extra code or execution time penalty.<\/p>\n<p>If you have any handy tips that you might like to share, please <a href=\"mailto:colin_walls@mentor.com\">email<\/a> or contact me via social media.<\/p>\n<p><a href=\"http:\/\/www.linkedin.com\/in\/colinwalls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6579\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/linkedin.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/twitter.com\/colin_walls\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6583\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/twitter.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"https:\/\/www.facebook.com\/colinwalls.author\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6591\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/facebook.png\" alt=\"\" width=\"40\" height=\"40\" \/><\/a><a href=\"http:\/\/blogs.mentor.com\/colinwalls\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-6599\" src=\"http:\/\/s3-blogs.mentor.com\/colinwalls\/files\/2014\/01\/wordpress.jpg\" alt=\"\" width=\"44\" height=\"44\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another in my occasional series of tips for embedded software developers. Sometimes my tips are just personal taste; on other&#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":[334,300,304,374,378,349,309],"industry":[],"product":[],"coauthors":[],"class_list":["post-9044","post","type-post","status-publish","format-standard","hentry","category-news","tag-dynamic-memory","tag-embedded-software","tag-nucleus","tag-nucleus-os","tag-nucleus-rtos","tag-overloading","tag-rtos"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9044","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=9044"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9044\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=9044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=9044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=9044"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=9044"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=9044"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=9044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}