Mail Archives: djgpp/1996/04/15/11:50:32
sam AT cs DOT vu DOT nl (Megens SA) wrote:
>Hi!
>I ran into the problem of aligned fields within a struct. I need to pack a
>struct so it will have exactly the memory-layout I expect it to be (as
>mentioned in the FAQ, section 22.9). I tried the example that was given in
>the info-package about __attribute__((packed)), but although that should
>work, it doesn't...
>A small program I wrote to test it:
><source on>
//
#pragma pack(1)
//
>struct foo
>{
//
char a __attribute__((packed));
//
> int x[2] __attribute__((packed));
>};
//
#pragma pack()
//
>int main()
>{
> foo* f = new foo;
> printf("size of struct = %d\n", sizeof(foo));
> printf("f = %p, a = %p, x = %p\n", f, &(f -> a), &(f -> x));
> return 0;
>}
><source off>
>This produces something like:
>size of struct = 12
>f = 4c0a0, a = 4c0a0, x = 4c0a4
>But what I wanted to see was:
>size of struct = 9
>f = 4c0a0, a = 4c0a0, x = 4c0a1
>To fix this, I could compile with -fpack-struct, but the docs say I
>shouldn't do that if I am using library-defined struct, which is of course
>the case. :) Another solution is to change the layout of the struct (as
>mentioned in the docs), but that also is not an option.
>I'm using DJGPP v2, gcc version 2.7.2 (everything else seems to work fine).
>I read the FAQ, I read the gnu-info docs about packed structs, but I still
>can't get it to work. Does someone know the solution to this?
>Thanks in advance!
>DGreetings,
>SAM
-An
- Raw text -