{"id":6972,"date":"2014-07-07T14:41:17","date_gmt":"2014-07-07T13:41:17","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=6972"},"modified":"2014-07-07T14:41:17","modified_gmt":"2014-07-07T13:41:17","slug":"uniqueness-of-c-methods-and-class-member-variables","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2014\/07\/07\/uniqueness-of-c-methods-and-class-member-variables\/","title":{"rendered":"Uniqueness of C++ methods and class member variables"},"content":{"rendered":"<p>I have made a number of <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2014\/06\/30\/c-yet-more-questions-and-answers\/\" target=\"_blank\" rel=\"noopener noreferrer\">recent postings<\/a> focused on C++ issues, responding to a number of questions. I have a few more planned, but I was intending to give this topic a rest for a while. However, my eye was caught by one question, which I felt had some potential:<\/p>\n<p>I learned that Objective C objects are not complete copies of the class object. In other words, only the data and certain static structures are independent for each instance of a class. The methods are kept in a central location for all objects to use. Is there any C++ implementation for embedded systems that use this feature?<\/p>\n<p>This sounds simple, but this question raises quite a few issues &#8230;<!--more--><\/p>\n<p>First off, Objective C is of limited interest to embedded developers. So a comparison with C++ may be of limited value.<\/p>\n<p>In C++, there is no such thing as a &#8220;class object&#8221; really. A class [or a struct] is a &#8220;recipe&#8221; to make objects. Defining a class\/struct does not create any code or data. Instantiating the class &#8211; i.e. defining objects [or variables, if you prefer] &#8211; is when real code and data is created\/allocated. It is at that point that some discussion about memory usage makes sense. Here is a simple class definition:<\/p>\n<pre>class example\n{\npublic:\n   int local_variable;\n   static int shared_variable;\n   void out_of_line_function(int p);\n   int inline_function(int x)\n   {\n      local_variable = x;\n      return x;\n   };\n};<\/pre>\n<p><\/p>\n<pre>void example::out_of_line_function(int p)\n{\n   shared_variable = p;\n}<\/pre>\n<p><\/p>\n<pre>example class_instantiation_1, class_instantiation_1;<\/pre>\n<p>Here we have a class &#8211; <strong>example<\/strong> &#8211; and an object instantiated from it &#8211; <strong>class_instantiation_1<\/strong> and <strong>class_instantiation_2<\/strong>.<\/p>\n<p><\/p>\n<div>\n<div><\/div>\n<div><a style=\"color: #a7a7a7;text-decoration: none;font-weight: normal !important;border: none\" href=\"http:\/\/www.gettyimages.com\/detail\/172515644\" target=\"_blank\" rel=\"noopener noreferrer\">#172515644<\/a> \/ <a style=\"color: #a7a7a7;text-decoration: none;font-weight: normal !important;border: none\" href=\"http:\/\/www.gettyimages.com\" target=\"_blank\" rel=\"noopener noreferrer\">gettyimages.com<\/a><\/div>\n<\/div>\n<p>Now we can look at each member variable and function [method] individually:<\/p>\n<ul>\n<li>The first member variable is <strong>local_variable<\/strong>. This is allocated space when an object is created and each object has its own copy of this variable. So, <strong>class_instantiation_1.local_variable<\/strong> refers to one memory location and <strong>class_instantiation_2.local_variable<\/strong> to another. This is, I believe , exactly what might be expected.<\/li>\n<li>The next member variable is <strong>shared_variable<\/strong>. As this is declared <strong>static<\/strong> the storage allocated to this variable is shared between every instance of the class. So, <strong>class_instantiation_1.shared_variable<\/strong> and <strong>class_instantiation_2.shared_variable<\/strong> refer to the same data.<\/li>\n<li>The first method in the class is <strong>out_of_line_function()<\/strong>. Normally, a single copy of this code will exist and be shared by all instances of the class. A separate copy of the code for each class instantiation would be a ridiculous waste of resources &#8211; the kind of thing that makes embedded developers lose sleep. However, there is the possibility that the compiler, if the optimization options allow, might decide to inline this function &#8211; i.e. place a copy of the code at each call site, instead of incurring the call\/return runtime overhead. This possibility cannot be determined from the code alone, but the inlining is only possible if this definition is in a header file [as the class definition would be], instead being in another .cpp file.<\/li>\n<li>The code for the second method &#8211; <strong>inline_function()<\/strong> &#8211; is defined within the class body, which is a shorthand way to tell the compiler that an inline optimization may be appropriate; it is equivalent to using the <strong>inline<\/strong> keyword. However, this is only a suggestion and, once again, the exploitation of this possibility cannot be determined from the code alone.<\/li>\n<\/ul>\n<p>Broadly speaking, the member variables of an object may be local or shared; this is determined by the programmer. Methods may be out of line [shared] or inline [sort of local], but this is determined largely by the compiler.<\/p>\n<p>Once again, I acknowledge the input from my colleague <a href=\"mailto:jonathan@codesourcery.com\" target=\"_blank\" rel=\"noopener noreferrer\">Jon Roelofs<\/a> on this blog posting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have made a number of recent postings focused on C++ issues, responding to a number of questions. I have&#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-6972","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\/6972","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=6972"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6972\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=6972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=6972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=6972"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=6972"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=6972"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=6972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}