-
packed structs
i'm reading into some structs from a file, so of course i need the structs aligned a certain way to guarantee that everything is read correctly. since i have gcc, i'm using __attribute__ ((packed)). however, all my structs work fine (afaik) except for this one, without the __attribute__ ((packed)), because i guess they are well aligned structs. but why does this one need __attribute__ ((packed)) to work well? or would you have to see more code to know?
PHP Code:
// behaves differently (does not load data from file as expected)
// without __attribute__ ((packed))
typedef struct
{
short xoff, yoff;
short patchnum;
short one, zero;
} __attribute__ ((packed)) patchentry_t;
-
btw, in the file, the shorts come in the order i just wrote them: xoff, yoff, patchnum, one, and zero.
-
> but why does this one need __attribute__ ((packed)) to work well?
No idea, because when I compile with DJGPP (also gcc), I get a size of 10 whether it is packed or not (packed makes no difference). This is what I would expect of a structure where all members have the same size and alignment.
I suggest you first print the sizeof() of this struct when it is packed and unpacked.
> however, all my structs work fine (afaik) except for this one
Perhaps this one is being broken by a previous struct (which should be packed) actually reading too much data.
I suggest you do something like this for each of your structures to make sure they are as you expect
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
typedef struct
{
short xoff, yoff;
short patchnum;
short one, zero;
} /*__attribute__ ((packed))*/ patchentry_t;
int main(void) {
printf( "%d\n", sizeof(patchentry_t) );
printf( "%d\n", offsetof(patchentry_t,xoff) );
printf( "%d\n", offsetof(patchentry_t,yoff) );
printf( "%d\n", offsetof(patchentry_t,patchnum) );
printf( "%d\n", offsetof(patchentry_t,one) );
printf( "%d\n", offsetof(patchentry_t,zero) );
return 0;
}
-
thanks, i didn't know the offsetof() thingy. i'll try it out. the sizeof() is right, but without packed the numbers appear to be scrambled. i'm not sure; it probably is other problems, i'll look into it.
-
In the first place, 'packing' only takes the padding bytes out of the structure. This is necessary in some cases (pascal is notorious for packed structures).
Code:
typedef struct
{
short xoff;
short yoff;
short patchnum;
short one;
short zero;
}patchentry_t;
This will use 10 bytes in most cases irregardless of packed or not because the fields already aligns on 16-bit word boundaries for all fields.
Most compilers align on 16-bit boundaries, unless specificly told to align on long word boundaries. It wastes too many bytes to align on long-word, unless it's necessary for performance.
What would be _really useful_ would be for you to post one of the structures out of the file-- just the raw hex bytes, for the first 20 bytes or so, and tell us what the fields are supposed to be. Then we could see how the structure in the field is aligned.
---
Normally, you don't need packing. If you have a structure you want saved to disk, simply use that structure as your buffer (pass its address and size to fwrite()). Then, you read it back in the same way, with fread()-- nothing is changed.