{"id":7028,"date":"2025-06-21T00:01:44","date_gmt":"2025-06-21T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7028"},"modified":"2025-06-14T17:33:03","modified_gmt":"2025-06-15T00:33:03","slug":"the-_alignof-keyword","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7028","title":{"rendered":"The <em>_Alignof<\/em> Keyword"},"content":{"rendered":"<p>The C11 standard added a few new keywords to the language. These are often called the &#8220;underscore keywords&#8221; because each is prefixed with an underscore. The second letter is often capitalized, as is the case with <em>_Alignof<\/em>.<br \/>\n<!--more--><br \/>\nI&#8217;m unsure of when the <em>_Alignof<\/em> operator is necessary, but obviously more than one coder needed it because it was added to the language in 2011. The keyword has been deprecated with the C23 standard, not because it&#8217;s dangerous or too weird but because it&#8217;s been replaced by the more official <em>alignof<\/em> operator.<\/p>\n<p>Here is the operator&#8217;s format:<\/p>\n<p><code>size_t _Alignof(<em>type name<\/em>);<\/code> (C11)<br \/>\n<code>size_t alignof(<em>type name<\/em>);<\/code> (C23)<\/p>\n<p>The <em>type name<\/em> is either a data type or an identifier (variable name). The value returned is an integer that describes the memory alignment of the type examined. To explain what all this means, you need to know how data is arranged in memory.<\/p>\n<p>The computer doesn&#8217;t store data at haphazard locations. Data is aligned at certain locations or offsets because the processor more efficiently fetches data in this manner. For example, less overhead is required to read a 16-bit integer when it&#8217;s aligned in memory at address 0x12A0 than were the integer stored at address 0x12A3. The <em>_Alignof<\/em> operator determines how the data is aligned based on its size in memory.<\/p>\n<p>The following code outputs three tidbits for three different data types.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_06_21-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_06_21-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n\/\/https:\/\/en.cppreference.com\/w\/c\/language\/object#Alignment\r\n#include &lt;stdio.h&gt;\r\n\r\nstruct alpha {\r\n    char c;\r\n    int i;\r\n};\r\n\r\nint main()\r\n{\r\n    struct alpha a;\r\n    char string[] = \"abcdefghijk\";\r\n    int beta = 0;\r\n\r\n    printf(\"The structure occupies %zu bytes\\n\",sizeof(struct alpha) );\r\n    printf(\"The structure aligns at %zu bytes\\n\",_Alignof(struct alpha) );\r\n    printf(\"The structure sits at location %p\\n\",&amp;a);\r\n    printf(\"The string occupies %zu bytes\\n\",sizeof(string) );\r\n    printf(\"The string aligns at %zu bytes\\n\",_Alignof(string) );\r\n    printf(\"The string sits at location %p\\n\",string);\r\n    printf(\"The integer occupies %zu bytes\\n\",sizeof(beta) );\r\n    printf(\"The integer aligns at %zu bytes\\n\",_Alignof(beta) );\r\n    printf(\"The integer sits at location %p\\n\",&amp;beta);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>For each data type &mdash; structure, string, and integers &mdash; three values are output: The size of the data in memory, the alignment, and its memory location. Here&#8217;s sample output on my system:<\/p>\n<p><code>The structure occupies 8 bytes<br \/>\nThe structure aligns at 4 bytes<br \/>\nThe structure sits at location 0x7ffe0c2c4e30<br \/>\nThe string occupies 12 bytes<br \/>\nThe string aligns at 1 bytes<br \/>\nThe string sits at location 0x7ffe0c2c4e24<br \/>\nThe integer occupies 4 bytes<br \/>\nThe integer aligns at 4 bytes<br \/>\nThe integer sits at location 0x7ffe0c2c4e20<\/code><\/p>\n<p>The structure aligns at 4 bytes, probably because the integer memory requires 4 bytes of storage, which is just a guess on my part.<\/p>\n<p>The string occupies 12 bytes, but can align at any location. Strings are character arrays, and a character can dwell at any specific location in memory without annoying the processor.<\/p>\n<p>The integer occupies 4 bytes and is aligned at 4 byte offsets.<\/p>\n<p>If I modify the structure to contain a <em>long<\/em> instead of an <em>int<\/em>, the first few lines of output appear as:<\/p>\n<p><code>The structure occupies 16 bytes<br \/>\nThe structure aligns at 8 bytes<br \/>\nThe structure sits at location 0x7ffcfc806038<br \/>\n...<\/code><\/p>\n<p>A <em>long<\/em> occupies 8 bytes of memory, so it&#8217;s aligned at 8-byte offsets. The structure expands to fill 16 bytes of memory, even though its other member is just a character. Figure 1 illustrates why this alignment is necessary and how it works.<\/p>\n<div id=\"attachment_7030\" style=\"width: 577px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7030\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/06\/0621-figure1.png\" alt=\"\" width=\"567\" height=\"394\" class=\"size-full wp-image-7030\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/06\/0621-figure1.png 567w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/06\/0621-figure1-300x208.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/06\/0621-figure1-432x300.png 432w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><p id=\"caption-attachment-7030\" class=\"wp-caption-text\">Figure 1. How a structure may align in memory due to its members&#8217; data types.<\/p><\/div>\n<p>From Figure 1, consider that the compiler knows that it must align a <em>long<\/em> member of the structure at an 8-byte boundary, which is why the structure occupies 16 bytes. The allocation shows some wasted space, but I believe it makes accessing the structure&#8217;s data more efficient as a trade-off.<\/p>\n<p>All this information is interesting to examine, and it may help you to understand how data is stored in computer memory. But to apply a practical application or need for the <em>_Alignof<\/em>\/<em>alignof<\/em> operator continues to puzzle me. Even so, it&#8217;s been considered necessary in C, even updated with the current C23 standard.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s another weirdo keyword that no one knows what it does &mdash; until now! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7028\">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-7028","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\/7028","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=7028"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7028\/revisions"}],"predecessor-version":[{"id":7036,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7028\/revisions\/7036"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}