{"id":26,"date":"2010-06-09T08:52:35","date_gmt":"2010-06-09T08:52:35","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=26"},"modified":"2022-02-17T10:13:11","modified_gmt":"2022-02-17T10:13:11","slug":"sizeof","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/memory\/sizeof","title":{"rendered":"sizeof"},"content":{"rendered":"\n<p>The sizeof operator yeilds the number of bytes required to store an object of the type of its operand.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tsizeof(char) \t\t\/\/ = 1\n\tsizeof(my_array) \t\t\/\/ = size of the array in bytes\n\tsizeof(my_structure) \t\/\/ = size of the structure in bytes\n\tsizeof(my_union) \t\t\/\/ = size of the union in bytes\n\n\tfor count = 0; count &lt; (sizeof(my_array) \/ sizeof(my_array_tye)); count++)\n\t{\n\t\t\/\/Do each entry in the array of a not necessarily known object size<\/code><\/pre>\n\n\n\n<p>Note that 16 and 32bit compilers may add padd bytes and these will be included in the sizeof &#8211; this can be a source of bugs!<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Getting array length from sizeof<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>int Prices&#91;5] = { 1, 2, 3, 4, 5 };\r\n\rint Length = sizeof(Prices) \/ sizeof(Prices&#91;0]);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Avoiding Padding<\/h4>\n\n\n\n<p>You can used the packed variable attribute. For example in the following code, type c_t will have a size of 3 bytes and there will be no wasted space between a and b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct\n{\n  unsigned char a&#91;1];\n} __attribute__ ((packed)) a_t;\n\ntypedef struct\n{\n  unsigned char b&#91;2];\n} __attribute__ ((packed)) b_t;\n\ntypedef struct\n{\n  a_t a;\n  b_t b;\n} __attribute__ ((packed)) c_t;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The sizeof operator yeilds the number of bytes required to store an object of the type of its operand. Note that 16 and 32bit compilers may add padd bytes and these will be included in the sizeof &#8211; this can be a source of bugs! Getting array length from sizeof Avoiding Padding You can used [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":1205,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/26\/revisions\/1205"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}