{"id":2468,"date":"2017-04-22T00:01:14","date_gmt":"2017-04-22T07:01:14","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2468"},"modified":"2017-04-15T09:09:54","modified_gmt":"2017-04-15T16:09:54","slug":"look-at-the-sizeof-that-thing","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2468","title":{"rendered":"Look at the <em>sizeof<\/em> That Thing"},"content":{"rendered":"<p>A structure is a multi-variable, containing several variable types, all members of the same unit. Two declarations are required for a structure: The structure itself and the variable. Programmers get into trouble with structures when determining the variable&#8217;s size, using <em>typedef<\/em> as a structure shortcut, and when declaring structure pointers.<br \/>\n<!--more--><br \/>\nIn my books, I recommend avoiding <em>typedef<\/em> when working with structures. Sure, <em>typedef<\/em> saves you some typing (though that&#8217;s not the &#8220;type&#8221; in <em>typedef<\/em>). Until you understand structures, it&#8217;s best to avoid <em>typedef<\/em>.<\/p>\n<p>The structure pointer issue is more complex. Basically, programmers working with linked lists declare a structure pointer before the structure itself is defined, which leads to all kinds of woe.<\/p>\n<p>That leaves the <em>sizeof<\/em> issue for this Lesson.<\/p>\n<p>The <em>sizeof<\/em> operator returns the storage size for a variable. That may seem like trivial information, but when you write data to a file, the <em>fwrite()<\/em> function requires a data-chunk size; the <em>sizeof<\/em> operator returns that value nicely. The problem with structures is on which item to use the <em>sizeof<\/em> operator: the structure itself, the structure variable, or the structure pointer?<\/p>\n<p>As an example, here is the <code>struct person<\/code>, which contains a few tidbits relating to a human:<\/p>\n<pre class=\"screen\">\r\n    struct person {\r\n        char first_name[24];\r\n        char last_name[24];\r\n        int age;\r\n    };<\/pre>\n<p>The structure contains two strings, <code>first_name<\/code> and <code>last_name<\/code> and an <em>int<\/em> value, <code>age<\/code>. My guess is that the size of this structure will be 24+24+4, or 52 bytes. That&#8217;s given that a typical <em>int<\/em> value on a modern computer is 4-bytes wide. (Though it&#8217;s bad to guess a structure size; always use <em>sizeof<\/em> instead.)<\/p>\n<p>The following code declares the <code>person<\/code> structure and returns its size:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct person {\r\n        char first_name[24];\r\n        char last_name[24];\r\n        int age;\r\n    };\r\n\r\n    printf(\"The structure 'person' has a size %lu\\n\",sizeof(struct person));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>The structure 'person' has a size 52<\/code><\/pre>\n<p>I guessed correctly!<\/p>\n<p>Remember that the structure itself is just a structure. Only when a variable is created are the 52 bytes of memory allocated. I can modify the code to create a <code>person<\/code> variable <code>donald<\/code>, and then use <em>sizeof<\/em> on that variable:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct person {\r\n        char first_name[24];\r\n        char last_name[24];\r\n        int age;\r\n    };\r\n    struct person donald;\r\n\r\n    printf(\"The structure 'person' has a size %lu\\n\",sizeof(struct person));\r\n    printf(\"The variable 'donald' has a size %lu\\n\",sizeof(donald));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the output:<\/p>\n<pre><code>The structure 'person' has a size 52\r\nThe variable 'donald' has a size 52<\/code><\/pre>\n<p>Declaring a structure variable instantly sets aside the proper quantity of memory, but that allocation doesn&#8217;t happen when you declare a structure pointer variable. This fact causes trouble for C programmers.<\/p>\n<p>In the final modification to the code, I add a structure pointer variable:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct person {\r\n        char first_name[24];\r\n        char last_name[24];\r\n        int age;\r\n    };\r\n    struct person donald;\r\n    struct person *george;\r\n\r\n    printf(\"The structure 'person' has a size %lu\\n\",sizeof(struct person));\r\n    printf(\"The variable 'donald' has a size %lu\\n\",sizeof(donald));\r\n    printf(\"The pointer variable 'george' has a size %lu\\n\",sizeof(george));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output shows that a pointer variable, even though it&#8217;s a pointer to a 52-byte structure, is only the size of the pointer variable:<\/p>\n<pre><code>The structure 'person' has a size 52\r\nThe variable 'donald' has a size 52\r\nThe pointer variable 'george' has a size 8<\/code><\/pre>\n<p>The pointer must be initialized before it can be used. For a structure, that involves using the <em>malloc()<\/em> function to allocate the proper quantity of storage, <code>sizeof(person)<\/code>, and then saving the memory location returned in the <code>george<\/code> variable. Even then, the pointer occupies only 8 bytes of storage where the structure allocated would be 52 bytes in size.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to properly obtain the size of a <em>struct<\/em> variable. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2468\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2468","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2468"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2468\/revisions"}],"predecessor-version":[{"id":2482,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2468\/revisions\/2482"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}