Hey. I am having difficulty understanding how memory is allocated to structural elements.
For example, if I have a lower structure and the size of char is 1 and int is 4 bytes respectively.
struct temp
{
char a;
int b;
};
I know that the size of the structure will be 8. Since 3 bytes will be added after the char, and the next element should be placed in several of 4, so the size will be 8.
Now consider the structure below.
struct temp
{
int a;
double b;
char c;
double d;
int e;
};
This is o / p I got for the above structure
size of node is 40
the address of node is 3392515152 ( =: base)
the address of a in node is 3392515152 (base + 0)
the address of b in node is 3392515160 (base + 8)
the address of c in node is 3392515168 (base + 16)
the address of d in node is 3392515176 (base + 24)
the address of e in node is 3392515184 (base + 32)
Total memory up to 36 bytes, why is it displayed as 40 bytes? If we create an array of such a structure, the first element of the next element of the array can be placed in 3392515188 (base + 36), since it is a multiple of 4, but why does it not happen this way?
Can anyone plz solve my doubts.
,
Saravana