title: Yet another confusion about data alignment and padding
body: <p>I've been reading about data alignment and structure padding.I've started from <a href="https://en.wikipedia.org/wiki/Data_structure_alignment" rel="nofollow noreferrer">here</a> and just to express my understanding, I can say an element or a data <code>e</code> of type <code>T</code> is naturally aligned if its address <code>a</code>is a multiple of its data size[<code>sizeof T</code>], </p>
  
  <p>let's consider</p>
  
  <pre><code>struct test{
   short a;
   char b;
  };
  </code></pre>
  
  <p><code>constexpr auto size = sizeof test</code> is 4 due to <code>struct test</code>'s max alignment(<code>short</code>)</p>
  
  <p>and then I went <a href="https://stackoverflow.com/questions/38875369/what-is-data-alignment-why-and-when-should-i-be-worried-when-typecasting-pointe">here</a>, the very first answer did a good explanation about data alignment, the author used this example </p>
  
  <pre><code>data1: "ab"
  data2: "cdef"
  
  |a b c d|     |e f 0 0|
  </code></pre>
  
  <p>Given the memory access granularity is 4 byte, I am going to make an assumption here like <code>data1</code> is of <code>2</code> byte and <code>data2</code> is of <code>4 byte</code>, since the <code>data2</code> begins at an odd address or its address isn't the multiple of <code>4 byte</code> which is considered as a  misaligned data, so accessing <code>data2</code> will have a different effect based on the architecture.</p>
  
  <p>here comes the padding to make <code>data2</code> to be naturally aligned by adding junkies(or pure 0s), at the end we have</p>
  
  <p><code>|a b 0 0|     |c d e f|</code></p>
  
  <p>So far fine, now let's wrap the two data' into a struct</p>
  
  <pre><code>struct wrap{
      data1: "ab"
      data2: "cdef"
  };
  </code></pre>
  
  <p>Now by considering the max alignment, the <code>wrap</code> size would become <code>8</code> with two-byte padding.</p>
  
  <p>here I am, in the case of <code>struct wrap</code> whether it is with padding or without padding the processor actually needs two cycles(given a WORD size is 4 byte) to fetch the <code>wrap</code> object right?
  so why do we need padding?</p>
  
  <p>without padding:</p>
  
  <p><code>|a b c d|     |e f 0 0|</code></p>
  
  <p>assume the processor fetched the 8 byte <code>wrap object</code>, <code>data1</code> is of two bytes so it can hold "ab" remaining will be truncated (just as how padded bits would be truncated)
  and <code>data2</code> can read consecutive 4 bytes from its address (which reads "cdef") rest were truncated, So why do we need padding here or I just got it wrong?</p>
  
  <p>the second question which more or less similar to above </p>
  
  <pre><code>struct test2{
      short a; //2 byte
      char _3,_4,_5,_6,_7,_8,_9,_10,_11 ;
  };
  </code></pre>
  
  <p>and the <code>sizeof test2</code> will give 12, given now the memory access granularity(changing WORD = 8) is 8-byte then the  <code>test2</code>'s object gonna occupy two WORD, again with or without padding it is going to occupy two WORD, so two cycles needed regardless of <code>test2</code>'s alignment to fetch its object, if this is the case why does padding matter here? why 12, not 11 though both needs two cycles in an 8-byte boundary?</p>
  
  <p>final question,</p>
  
  <pre><code>struct final{
    char a;
    int b;
  };
  </code></pre>
  
  <p>let's say the <code>struct final</code> is packed(no alignment) and the var <code>test::b</code> going to be placed at the odd address</p>
  
  <p>memory ( just a theoretical representation)</p>
  
  <pre><code>0          1          2          3          4          5          6         7
  tets::a:1  test::b:1  test::b:2  test::b:3  test::b:4
  </code></pre>
  
  <p><code>test::b:x</code> am not meaning bit fields here</p>
  
  <p>I've learned that as long I access test::b using object via `. or ->' it will be fine but when I take the address of it</p>
  
  <p><code>int * p = &amp;(test::b) ; (void)*p;</code> this statements will either take a performance hit or crash.</p>
  
  <p>Again if the WORD = 8 bytes then the <code>struct final</code>'s object  will get into memory in one cycle and let's say the pointer <code>p</code> holds the address <code>1</code> which is odd, but can't just the deference of <code>*p</code> copy the bits starting from address <code>1</code> up to its pointing to element type( 4-byte )into memory? why there is a problem here? what I've been missing to account for?</p>