{"id":9140,"date":"2019-11-11T11:43:14","date_gmt":"2019-11-11T10:43:14","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=9140"},"modified":"2026-03-26T16:58:30","modified_gmt":"2026-03-26T20:58:30","slug":"3-more-embedded-software-tips","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2019\/11\/11\/3-more-embedded-software-tips\/","title":{"rendered":"3 more embedded software tips"},"content":{"rendered":"<p>In my occasional series of tips for embedded software development, I have three more. This time they are all associated with C\/C++ programming \u2026<!--more--><\/p>\n<p>A lot of embedded programming is about paying attention to details, particularly when using the power of the C language, when there is no safety catch. \ud83d\ude42<\/p>\n<h3>Take care to ensure that an array index never goes negative<\/h3>\n<p>Arrays in C\/C++ start from zero and go up to <strong>n-1<\/strong> where <strong>n<\/strong> is the size of the array. This is straightforward, but there are two issues to think about: underflow and overflow. And these do need to be considered as there is no \u201crun time system\u201d to stop you making mistakes. First, overflow. It is not too hard to avoid running off of the end of array, it just needs code like this:<\/p>\n<pre><strong>#define N 99<\/strong>\n<strong>int arr[N];<\/strong>\n<strong>for (i=0; i&lt;N; i++)<\/strong>\n<strong>   arr[N] = i;<\/strong><\/pre>\n<p>Just make sure that <strong>&lt;<\/strong> is never replaced by <strong>&lt;=<\/strong> \u2026<\/p>\n<p>To me, starting counting at zero is totally intuitive, but I started out with assembly language programming, where that is obvious. Just do not let an index get negative. This code is entirely valid:<\/p>\n<pre><strong>arr[-5] = 99;<\/strong><\/pre>\n<p>but stupid.<\/p>\n<h3>In C, do not set the size of an array that will hold a string; let the compiler do it<\/h3>\n<p>When you create an array in C that is going to hold a text string, you have two things to think about. First, you need to be sure that you know how\/when the data will be placed into the array. Most embedded development toolkits take care of that, but you should be sure that you know what is going on.<\/p>\n<p>The second point is array size. You could write this:<\/p>\n<pre><strong>char str[13] = \u201cHello world!\u201d;<\/strong><\/pre>\n<p>That would be fine.There are 12 actual characters and a <strong>NULL<\/strong> terminator. Alternatively, this is easier:<\/p>\n<pre><strong>char str[] = \u201cHello world!\u201d;<\/strong><\/pre>\n<p>The compiler can count! Interestingly, you can also do this:<\/p>\n<pre><strong>char str[12] = \u201cHello world!\u201d;<\/strong><\/pre>\n<p>which creates a string without a <strong>NULL<\/strong> terminator. This would save a byte of memory, if you knew the string length elsewhere. Note that this is illegal in C++.<\/p>\n<p>If a text string is never going to be changed, there is no point in it being in a program section that is [logically] in RAM and this code might be better:<\/p>\n<pre><strong>char *str = \u201cHello world!\u201d;<\/strong><\/pre>\n<h3>Never assume the size of data types in C\/C++<\/h3>\n<p>The traditional data types in C &#8211; <strong>int<\/strong>, <strong>unsigned<\/strong>, <strong>char<\/strong>, etc. &#8211; are really non-ideal for most embedded applications, when the actual size of data is [or needs to be] know. It is much better to define typedefs with meaningful names &#8211; like <strong>UINT32<\/strong> &#8211; or use a newer C language variant and utilize the rational data types like <strong>uint32_t<\/strong>.<\/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>In my occasional series of tips for embedded software development, I have three more. This time they are all associated&#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,339,300],"industry":[],"product":[],"coauthors":[],"class_list":["post-9140","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-development-tools","tag-embedded-software"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=9140"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9140\/revisions"}],"predecessor-version":[{"id":10838,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9140\/revisions\/10838"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=9140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=9140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=9140"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=9140"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=9140"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=9140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}