title: memory alignment within gcc structs
body: <p>I am porting an application to an ARM platform in C, the application also runs on an x86 processor, and must be backward compatible.</p>
  
  <p>I am now having some issues with variable alignment. I have read the gcc manual for 
  <code>__attribute__((aligned(4),packed))</code> I interpret what is being said as the start of the struct is aligned to the 4 byte boundry and the inside remains untouched because of the packed statement.</p>
  
  <p>originally I had this but occasionally it gets placed unaligned with the 4 byte boundary.  </p>
  
  <pre><code>typedef struct  
  {  
   unsigned int code;  
   unsigned int length;  
   unsigned int seq;  
   unsigned int request;  
   unsigned char nonce[16];  
   unsigned short  crc;  
  } __attribute__((packed)) CHALLENGE;
  </code></pre>
  
  <p>so I change it to this.  </p>
  
  <pre><code>typedef struct  
  {  
   unsigned int code;  
   unsigned int length;  
   unsigned int seq;  
   unsigned int request;  
   unsigned char nonce[16];  
   unsigned short  crc;  
  } __attribute__((aligned(4),packed)) CHALLENGE;
  </code></pre>
  
  <p>The understand I stated earlier seems to be incorrect as both the struct is now aligned to a 4 byte boundary, and and the inside data is now aligned to a four byte boundary, but because of the endianess, the size of the struct has increased in size from 42 to 44 bytes. This size is critical as we have other applications that depend on the struct being 42 bytes. </p>
  
  <p>Could some describe to me how to perform the operation that I require. Any help is much appreciated.</p>