[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Programming question: sizeof struct?



Carl Mummert <mummert@cs.wcu.edu> writes:

> >#pragma pack(1)
> >struct {};
> >#pragma pack()
> >
> >Which forces the layout to be as you specified.
> >
> >Using a command line option is a Bad Idea (tm) as it may corrupt glibc's
> >structures
> 
> To test a resonse to the original message, I made the follwing c file
> ( I was not familiar with the attribute flag, so I guessed wrong):
> --begin
> 
> __attribute__ ((packed)) struct foo 
struct foo __attribute__ ((packed)) ???
> {
>   char c[3];
>   int x;
> };
> 
> int main()
> {
>   printf("%d \n", sizeof(struct foo));
> }
> --end
...
> Which made me think... and check... and sure enough
> the ORIGINAL STRUCT actually has a size of 7 with the command line option!
> THe guy is either crazy, or he is using some strange compiler that
> we don't know about (althoguh he did say the size was 6, as if he
> had a 286...)

Maybe he meant

struct foo {
	char c[3];
	short s;
	// char __PAD;
}

or

struct ofoo {
	char c[3];
	// char __PAD;
	short
}

as any normal compiler would generate.

MfG,
	Goswin


Reply to: