CMP EMBEDDED.COM

Login | Register     Welcome Guest  
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

Padding and rearranging structure members
Here's what C and C++ compilers must do to keep structure members aligned.



Embedded.com

As another example, suppose that gadget is a structure defined as:

struct gadget
{
    char m1;
    double m2;
    char m3;
};

and that objects of type double must be double-word aligned. The compiler will add 7 bytes of padding after member m1 and also after member m3, as if the structure had been defined as:

struct gadget
{
    char m1;
    char padding_after_m1[7];
    double m2;
    char m3;
    char padding_after_m3[7];
};

The most strictly aligned member of gadget is m2, which is double-word aligned. Thus, each gadget must also be double-word aligned. The padding after m1 ensures that member m2 is double-word aligned relative to the beginning of each gadget. The padding after m3 ensures that the next gadget in an array of gadgets is also double-word aligned.

1 | 2 | 3 | 4 | 5

Rate this article: Low High
Current rating
  • .
Embedded.com Career Center
Looking for a new job?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS



TECH PAPER
WEBINAR
WEBINAR
WEBINAR




 :