Why #pragma pack not take effect?

J

Jimmy

Struct definition as following(on 32-bit Linux):

#pragma pack(push, 8)
struct MY_STRUCT
{
char a[2];
short b;
short c;
short d;
int e;
long long x;
long long y;
};
#pragma(pop)

During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

If I get wrong usage of #pragma pack(), could anyone please tell me how to
get it work correctly?

BTW, what on earth is the difference between __attribute__ align() and
#pragma pack()?

Thanks in advance!
 
K

Keith Thompson

Jimmy said:
Struct definition as following(on 32-bit Linux):

#pragma pack(push, 8)
struct MY_STRUCT
{
char a[2];
short b;
short c;
short d;
int e;
long long x;
long long y;
};
#pragma(pop)

During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

If I get wrong usage of #pragma pack(), could anyone please tell me how to
get it work correctly?

BTW, what on earth is the difference between __attribute__ align() and
#pragma pack()?

Neither __attribute__ align() nor #pragma pack is defined by the C
standard. You'll need to check your compiler's documention, or ask in
a forum where it's discussed.

Incidentally, I would have assumed that "#pragma pack(push, 8)" would
be paired with "#pragma pack(pop)", not "#pragma(pop)". If my guess
is correct (and it's only a guess), then what you've posted isn't your
real code. If you're going to post code, *always* copy-and-paste the
exact code.
 
A

Army1987

Jimmy said:
Struct definition as following(on 32-bit Linux):

#pragma pack(push, 8)
struct MY_STRUCT
{
char a[2];
short b;
short c;
short d;
int e;
long long x;
long long y;
};
#pragma(pop)

During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

If I get wrong usage of #pragma pack(), could anyone please tell me how to
get it work correctly?

Not here. It's implementation defined. Check the documentation of
your compiler.
 
S

slebetman

Struct definition as following(on 32-bit Linux):
During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

This is OT here but anyway... assuming this is gcc then 28 bytes is
correct, I don't know why you'd expect otherwise. Note that for x86
gcc usually defines:
char: 1 byte
short: 2 bytes
long: 4 bytes
int: 4 bytes
long long: 8 bytes

Therefore, your struct is:

struct MY_STRUCT
{
char a[2]; /* 2 */
short b; /* 2 */
short c; /* 2 */
short d; /* 2 */
int e; /* 4 */
long long x; /* 8 */
long long y; /* 8 */
/* total: 28 */
};

YMMV since gcc can be modified and recompiled to have different sizes
for variables. But for the sake of being able to correctly compile the
Linux TCP/IP stack this is the most common configuration on most 32bit
(and even most newer 64bit) machines.
 
C

CBFalconer

Jimmy said:
.... snip ...

BTW, what on earth is the difference between __attribute__ align()
and #pragma pack()?

None. Neither exist in standard C. Find a suitable newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top