Mail Archives: djgpp/1998/04/22/21:30:29
massarin AT alpha3 DOT cram DOT enel DOT it wrote in article
<6hi61a$avd1 AT willy DOT cra DOT enel DOT it>...
> My problem is that when I declare a struct like:
>
> struct a {
>
> char b[n];
> int c;
> . }
>
> if n (number of char in array) is not a multiple of 4
> the array is enlarged to fit a 4' multiple,
>
> Does anyone know how to make the struct, and the array, of
> the proper size ,apart from doing the struct as an array ?
> Is it a bug of compiler ?
>
> Thanks.
>
> ps. Please answer by newsgroup .
>
>
It's not a bug, it's an optimization! The processor always read memory at
word aligned addresses. Thus, to improve performance, a datum should never
begin on odd address. On 486 and Pentium processor, the data should be
aligned at 8 byte and procedure at 16 byte (the cache fetch at 0-mod-16
addresses).
Two way to pack structures:
1. Use compiler switch "-fpack-struct": A bad solution since You'll pack
everithing (degraded performance)
2. Use __attribute__((paked)) (Info gcc.info, node "Variable Attributes"):
struct a {
char b[n];
int c __attribute__((packed));
}
- Raw text -