Memory alignment in C

Posted by user1758245 on Stack Overflow See other posts from Stack Overflow or by user1758245
Published on 2012-11-26T05:01:02Z Indexed on 2012/11/26 5:03 UTC
Read the original article Hit count: 125

Filed under:
|
|

Here is a snippet:

#pragma pack(4)
struct s1
{
    char a;
    long b;
};
#pragma pack()

#pragma pack(2)
struct s2
{
    char c;
    struct s1 st1;
};
#pragma pack()


#pragma pack(2)
struct s3
{
    char a;
    long b;
};
#pragma pack()

#pragma pack(4)
struct s4
{
    char c;
    struct s3 st3;
};
#pragma pack()

I though sizeof(s4) should be 10 or 12. But it turns out to be 8. I am using Visual C++ 6.0. Could someone tell me why?

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory