{"id":1066,"date":"2010-08-30T16:11:24","date_gmt":"2010-08-30T15:11:24","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=1066"},"modified":"2010-08-30T16:11:24","modified_gmt":"2010-08-30T15:11:24","slug":"c-at-fault","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2010\/08\/30\/c-at-fault\/","title":{"rendered":"C++ at fault"},"content":{"rendered":"<p>I like being right. Who does not? I am also interested in programming languages. Part of their appeal is that, unlike almost all spoken languages, they have a precise definition or specification to which implementers endeavor to comply. Many languages, like C and C++, have ANSI\/ISO standards, which nail down many of the details. These standards tend to be imperfect and aspects of the language can remain rather insecure, as implementers have too much latitude. This is why organizations such as MISRA publish detailed usage guidelines to avoid the problem areas. C++, in particular, gives some interesting opportunities to write obscure code, which I have written about here <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2010\/02\/01\/overloading-or-obfuscation\/\" target=\"_blank\" rel=\"noopener noreferrer\">before<\/a>.<\/p>\n<p>I was concerned, when I first started to learn C++, that there was a flaw in the language. Some years later, I got the whole story &#8230;<!--more--><\/p>\n<p>The C++ language is generally more &#8220;picky&#8221; than C. There are tighter requirements to specify data types and use them in a consistent manner. This is good, as it reduces opportunities for programmer error. When I was learning the language, I was a little confused by constructors and destructors. In C++, there is the concept of a class. This is a construct, very like a C language struct, but it can contain both code and data. Defining a class, in effect, adds a new data type to the language. There is the opportunity to include two &#8220;member functions&#8221;, which are executed when an object is instantiated from the class and when it goes out of scope; the constructor and destructor respectively. Here is an outline of a class definition:<\/p>\n<pre style=\"padding-left: 30px\">class fubar\n{\n   ...\npublic:\n   fubar();\n   ~fubar();\n   ...\n};<\/pre>\n<p>The two functions, <strong>fubar()<\/strong> and <strong>~fubar()<\/strong> are the constructor and destructor. Here I have just shown the declaration &#8211; the actual code is defined elsewhere.<\/p>\n<p>Neither of these functions has the opportunity to return a result. So, they are intrinsically <strong>void<\/strong>. But you will notice that I have not declared them as such. In fact, if I did, I would probably get an error from the compiler. This seems illogical. In C, a function without a specified return type is, by default, <strong>int<\/strong>. In C++, it is normally forbidden to exclude the type specification, but not here. Confusing eh?<\/p>\n<p>Some years later I got the answer to this mystery. In the early 1990s, I was working for Microtec, who were later acquired by Mentor Graphics. We had just released some of the first tools for programming embedded systems in C++ and we were promoting them and the broader use of the language. We had the idea that we would run a large seminar on C++ for embedded and, as one of our engineering team was rather well connected, we arranged for Bjarne Stroustrup, the inventor of the C++ language, to come along and speak. Needless to say, this attracted a large crowd, as we had planned. That evening, we took the Great Man out for dinner. Naturally, the conversation included some discussion of C++ and, although I was nervous about making a fool of myself, I had the chance to raise this anomaly. &#8220;Yes,&#8221; he said &#8220;we screwed up there really, but it is too late to go back and change it now.&#8221;<\/p>\n<p>It was very satisfying to get the answer &#8220;from the horse&#8217;s mouth&#8221;. I suspect many modern compilers allow a <strong>void<\/strong> declaration. Maybe even the spec has been revised. But I think that it illustrates the point that the only silly question is the one you do not ask.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like being right. Who does not? I am also interested in programming languages. Part of their appeal is that,&#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-1066","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\/1066","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=1066"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=1066"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=1066"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=1066"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}