In an application I was writing I found that defining the same structure two
different ways the memory footprint of the resulting structures was different.
If I used
typedef struct {...} name __attribute__ ((packed));
the resulting structure would be larger than if I did
struct name {...} __attribute__ ((packed));
The latter definition creates the correct output.
Here is a test program that brings out the error.
I compiled this with "gcc -c memory.c" on an NT4 SP5 system.
If you compile to assembly you can see where the "char" variable in test1 is assigned two
bytes of space instead of one.
// memory.c
// Justin Bandholz
// bandholz@vt.edu
// Differences between memory footprints of identical packed structures
#include <stdio.h>
typedef struct {
unsigned char test1;
unsigned short test2;
} test1 __attribute__ ((packed));
struct test2 {
unsigned char test1;
unsigned short test2;
} __attribute__ ((packed));
int main(void)
{
test1 varOne;
struct test2 varTwo;
varOne.test1 = 0;
varOne.test2 = 0;
varTwo.test1 = 0;
varTwo.test2 = 0;
printf("size test1:%d size test2:%d\n", sizeof(test1), sizeof(struct test2));
}
Justin Bandholz
bandholz@vt.edu