<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thach Pham on Medium]]></title>
        <description><![CDATA[Stories by Thach Pham on Medium]]></description>
        <link>https://medium.com/@hitsoda?source=rss-6c3e145fdfe7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*EH05bXXT8exPCCDem3XD-Q.png</url>
            <title>Stories by Thach Pham on Medium</title>
            <link>https://medium.com/@hitsoda?source=rss-6c3e145fdfe7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 21:08:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hitsoda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[C/C++ Struct Padding]]></title>
            <link>https://hitsoda.medium.com/c-c-struct-padding-fab7c6ae4e11?source=rss-6c3e145fdfe7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fab7c6ae4e11</guid>
            <category><![CDATA[do]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[struct]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Thach Pham]]></dc:creator>
            <pubDate>Sat, 12 Jun 2021 14:33:56 GMT</pubDate>
            <atom:updated>2021-07-15T12:12:23.702Z</atom:updated>
            <content:encoded><![CDATA[<p>A practical blog about struct padding in C/C++</p><h4><strong>What is struct padding ?</strong></h4><p>Padding is a concept that the compiler inserts some extra spaces between members of a struct for alignment. The alignment follows the below rules:</p><ul><li>Size of the whole struct is divisible by size of the largest individual member in the struct. If the largest member’s size is 4, the size of the struct should be 4xN such as 0, 4, 8, 12, 16, … .</li><li>Each individual member must be located at an address that is divisible by its size. A long member must be located at an address divisible by 8. Anintmember must start at an address divisible by 4. A short one must start at an even address. A char member can start at any address.</li></ul><h4><strong>Example</strong></h4><p>The below code creates a struct and write it to stdout.</p><pre>#include &lt;unistd.h&gt;<br>#include &lt;string.h&gt;</pre><pre>struct mystruct {<br>    char a;<br>    int b;<br>};</pre><pre>int main(int argc, char** argv){<br>    struct mystruct mst;<br>    memset(&amp;mst, 0, sizeof(mst));<br>    mst.a = 0xff;<br>    mst.b = 0xffffffff;<br>    write(STDOUT_FILENO, &amp;mst, sizeof(mst));<br>}</pre><p>Dump memory of mystruct by using command od -t x1 -An which read bytes from stdout one by one and print them as hex.</p><pre>$ g++ demo.cpp -o demo &amp;&amp; ./demo | od -t x1 -An<br>ff 00 00 00 ff ff ff ff</pre><p>The below figure illustrate the memory of mystruct .</p><pre>0       1       2       3       4       5       6       7       8<br>+-------+-------+-------+-------+-------+-------+-------+-------+<br>|a=0xff |    padding=0x000000   |           b=0xffffffff        |<br>+-------+-------+-------+-------+-------+-------+-------+-------+</pre><ul><li>The largest member of mystruct is b whose size is 4 so the compiler aligns the memory in the form of 4 bytes.</li><li>The first member, a is located at address 0.<br>The next one, b can not be allocated next to a, address 1. Because its size is 4, it must start at an address divisible by 4. Therefore, the compiler inserts 3 bytes padding = 0x000000 to puts b at address 4.</li></ul><p>Additionally, we can dump the memory as table with below command.</p><pre>$ g++ demo.cpp -o demo &amp;&amp; ./demo | od -t x1 -w4 -An<br>ff 00 00 00<br>ff ff ff ff</pre><pre>Illustrate<br>0           1           2           3           4<br>+-----------+-----------+-----------+-----------+<br>| a = 0xff  |           padding = 0x000000      |<br>+-----------+-----------+-----------+-----------+<br>|                  b = 0xffffffff               |<br>+----------+------------+----------+------------+</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fab7c6ae4e11" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>