Lets take a look at the following structs
:
struct child {
int a:1;
int b:2;
int c:2;
} __attribute__((packed));
struct parent1 {
int x:3;
struct child y;
} __attribute__((packed));
struct parent2 {
int p:1;
int q:5;
int r:5;
struct child s;
} __attribute__((packed));
These are the sizes I am getting:
sizeof(int) 4
sizeof(struct child) 1
sizeof(struct parent1) 2
sizeof(struct parent2) 3
I've heard that padding is added before structs for performance reasons. But forgetting about performance for a moment, is there a way so that I can get the following sizes?
sizeof(struct parent1) 1
sizeof(struct parent2) 2
As only that much of memory is actually required...
EDIT
Is there any way of doing it with gcc
on linux
?