{"id":5048,"date":"2021-11-13T00:01:28","date_gmt":"2021-11-13T08:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5048"},"modified":"2021-11-06T10:36:34","modified_gmt":"2021-11-06T17:36:34","slug":"declaring-structures-trick-3","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5048","title":{"rendered":"Declaring Structures, Trick #3"},"content":{"rendered":"<p>Bitfields in a structure are weird, as I covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5030\">last week&#8217;s Lesson<\/a>. If you&#8217;re a nerd who appreciates bits and bit manipulation, you&#8217;re probably in love. These bitwise  tricks are things the C language excels at. With a keen knowledge of bits, and a desire to use integer values beyond the standard widths, a nerd can have a lot of fun in C.<br \/>\n<!--more--><br \/>\nOne of the more unusual bitfield constructions I&#8217;ve seen appears in the C standard documentation. It looks like this:<\/p>\n<p><code>struct {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;char a;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int b:5, c:11,:0, d:8;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;struct { int ee:8; } e;<br \/>\n}<\/code><\/p>\n<p>Above you see a structure with a <em>char<\/em> member <code>a<\/code>, plus a bitfield with three variables, <code>b<\/code>, <code>c<\/code>, and <code>d<\/code>. The third member is a nested structure with a single 8-bit bitfield. I want to focus on middle line:<\/p>\n<p><code>int b:5, c:11,:0, d:8;<\/code><\/p>\n<p>Integer member <code>c<\/code> is set to a width of 11 bits, then you see a comma and <code>:0<\/code>. This curious item isn&#8217;t a member of the structure, nor is it somehow attached to variable <code>c<\/code>. The <code>:0<\/code> item is used for alignment.<\/p>\n<p>All data stored in memory is aligned, either at 32-bit or 64-bit intervals depending on system architecture. This alignment doesn&#8217;t mean that each character in a string occupies a 32-bit (four byte) chunk of memory. No, but that string itself starts at a 32-bit boundary.<\/p>\n<p>What the <code>:0<\/code> item does in a structure is to align the bitfield value that follows it with the next memory boundary.<\/p>\n<p>To demonstrate, I concocted the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_11_13-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_11_13-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    struct bitfield {\r\n        unsigned a:1, b:7;\r\n    };\r\n    struct bitfield align;\r\n\r\n    align.a = 1;\r\n    align.b = 15;\r\n    printf(\"size of align = %lu\\n\",sizeof(align));\r\n    printf(\"align.c = %u\\n\",align.a);\r\n    printf(\"align.d = %u\\n\",align.b);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>bitfield<\/code> structure contains two <em>unsigned<\/em> members, <code>a<\/code> and <code>b<\/code>. The first is a single bit, the second consists of a bitfield of 7 bits.<\/p>\n<p>Values are assigned at Lines 10 and 11. At Line 12, the size of the <code>align<\/code> structure is output. Then the structure&#8217;s member values are output:<\/p>\n<p><code>size of align = 4<br \/>\nalign.c = 1<br \/>\nalign.d = 15<\/code><\/p>\n<p>The <code>align<\/code> structure occupies four bytes of storage, 32 bits.<\/p>\n<p>You can&#8217;t see it, but for efficiency&#8217;s sake, both members <code>align.c<\/code> and <code>align.d<\/code> are stored in the same byte. If I add a <code>:0<\/code> to the structure definition, alignment is forced:<\/p>\n<p><code>unsigned a:1, :0, b:7;<\/code><\/p>\n<p>The above modification to Line 6 adds the <code>:0<\/code> alignment item to the definition. Now both values are stored in different 32-bit chunks of memory; member <code>b<\/code> is aligned. Here is the updated output:<\/p>\n<p><code>size of align = 8<br \/>\nalign.c = 1<br \/>\nalign.d = 15<\/code><\/p>\n<p>Due to the <code>:0<\/code> thingie, the <code>align<\/code> structure now occupies 64-bits of memory, two 32-bit chunks for eight bytes. Member <code>a<\/code> is stored in one chunk and member <code>b<\/code> in the other. The bit values aren&#8217;t shared in a single byte. This effect is what you get when a <code>:0<\/code> is specified in the structure&#8217;s definition.<\/p>\n<p>Bit fields are designed to save memory. Because memory is abundant in today&#8217;s systems, seeing bitfields used in a structure is rare. Even rarer is the need to align the bitfields, though if the need arises you now know the secret.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another weird thing with bitfields is that you can force their alignment in memory. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5048\">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":[23,22,21,26,25,24],"class_list":["post-5048","post","type-post","status-publish","format-standard","hentry","category-main","tag-alignment","tag-bitfield","tag-c","tag-c-language","tag-c-programming"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5048","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=5048"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5048\/revisions"}],"predecessor-version":[{"id":5058,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5048\/revisions\/5058"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}