473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why #pragma pack not take effect?

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!
Jul 3 '07 #1
3 4732
On Jul 3, 3:14 pm, "Jimmy" <lofe...@yahoo.com.cnwrote:
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.

Jul 3 '07 #2
Jimmy wrote:
>
.... snip ...
>
BTW, what on earth is the difference between __attribute__ align()
and #pragma pack()?
None. Neither exist in standard C. Find a suitable newsgroup.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 3 '07 #3
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Jimmy wrote:
>>
... snip ...
>>
BTW, what on earth is the difference between __attribute__ align()
and #pragma pack()?

None. Neither exist in standard C. Find a suitable newsgroup.
IOW:
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Jul 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JustSomeGuy | last post by:
I need to define a structure that is byte packed. I was planning on using #pragma pack (1) So when does the pragma pack end right after the first structure definition or at the end of the...
10
by: Steven T. Hatton | last post by:
Stroustrup says this: http://www.research.att.com/~bs/bs_faq2.html#macro "So, what's wrong with using macros?" "And yes, I do know that there are things known as macros that doesn't...
6
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
1
by: Gustavo L. Fabro | last post by:
Greetings! Going directly to the point: myclass.h: //-------------------------------------- #pragma managed //Forward declaration
1
by: Mike Margerum | last post by:
I seem to be running into an issue where I am deriving a new class from a struct with a different alignemnt via the pragma pack #pragma pack(1) typedef struct { unsigned short a; unsigned...
15
by: muttaa | last post by:
Hello all, I'm a beginner in C...May i like to know the difference between a #pragma and a #define.... Also,yet i'm unclear what a pragma is all about as i can find topics on it only in...
5
by: venkat | last post by:
Hi, I have come across the a pragma definition " #pragma switch direct ". I don't know any thing about pragma's. Even i when i search through google not getting exact information. what does...
6
by: Boltar | last post by:
If I use #pragma pack to byte align structures to non word size , will this cause the program to run slightly slower if it is accessing and setting non word aligned integers etc in the struct? Is...
4
by: Steven Woody | last post by:
In our project, one used the following struct #pragma pack(1) struct Foo { uint8_t b1; uint8_t b2; uint8_t b3; std::vector<uint8_t buf; ... };
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.