I have some question on structure padding
Can any one help me!
Structure padding is done for alignment(is it right)
1. Why is it to be aligned?
2. struct sfoo
{
int a;
char i;
}
sizeof(struct sfoo) = 8 // 4+1+3(padding)
struct sfoo
{
//int a;
char i;
}
sizeof(struct sfoo) = 1 // no padding
Is struct sfoo aligned? if yes/no why?
for a structure to be aligned, is it required that the toatal size must be a multiple of 2/4/8
3. Is to possible to store any value in the padded bits?
----End---0f---section--one
this macro usedto find the offset of a varable in stucture
#define offsetof(s,m) (size_t)&(((s *)0)->m)
((s *)0): takes the integer zero and casts it as a pointer to s.
((s *)0)->m: dereferences that pointer to point to structure member m.
&(((s *)0)->m): computes the address of m.
(size_t)&(((s *)0)->m): casts the result to an appropriate data type.
1. what value shld i pass to size_t?
I know size_t is an unsigned integer returned by sizeof operator so what is the parameter passed to sizeof()
2. when i remove '&' from the macro i got a segfault, why?
Is it because of '0'. but even thought if we use '&' its must give a segfault because of '0'
but its not.
3. how come this macro give the result in nuber even though it is converted to *s
----End----of----Section---two
Is there any macro to disable structure padding?
Thank you for your time and help