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

> Is struct sfoo aligned? if yes/no why?
structs are always aligned. They're usually aligned to match the alignment of the member with the most restrictive alignment.
By removing the int, you removed the need to make the struct 4-byte aligned, so it just dropped back to the alignment for a char.

> 3. Is to possible to store any value in the padded bits?
Not in any portable manner it isn't.

> 1. what value shld i pass to size_t?
You use offsetof like posOfMember = offsetof( struct foo, aMember ); > 2. when i remove '&' from the macro i got a segfault, why?
Because you really do end up dereferencing a NULL pointer!

> Is there any macro to disable structure padding?
That depends on your compiler, so you need to read the manual for your compiler to find out.
Some compilers use #pragma to achieve this.

> Is struct sfoo aligned? if yes/no why?
structs are always aligned. They're usually aligned to match the alignment of the member with the most restrictive alignment.
By removing the int, you removed the need to make the struct 4-byte aligned, so it just dropped back to the alignment for a char.

> 3. Is to possible to store any value in the padded bits?
Not in any portable manner it isn't.

> 1. what value shld i pass to size_t?
You use offsetof like posOfMember = offsetof( struct foo, aMember ); I mean not the offsetof function but to the macro which i have mentioned (size_t)&(....
> 2. when i remove '&' from the macro i got a segfault, why?
Because you really do end up dereferencing a NULL pointer!

> Is there any macro to disable structure padding?
That depends on your compiler, so you need to read the manual for your compiler to find out.
Some compilers use #pragma to achieve this.

Thank you for ur information

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.