On Sat, Aug 6, 2011 at 10:07 AM, Sabya Sachi <sachi1...@gmail.com> wrote:

> At many places, it has been mentioned that in C, a structure (when not
> packed) pads its content so as to facilitate memory alignment so that
> its members are accessed faster.
> like for e.g the snippet
>
> struct node
> {
> char d;
> int a;
> };
> int main(){
>    struct node a;
>    printf("size = %d",sizeof(a));
> }
> gives the output 8 as it pads the char to 4 bytes.
>
>


But the following snippet outputs 4
>
> struct node
> {
> char d:1;
> int a:1;
> };
> int main(){
>    struct node a;
>    printf("size = %d",sizeof(a));
> }
>
> I thought that maybe it was padding upto the nearest multiple of
> sizeof(int), but then the following snippet gave the output 1.
>



in both of the code above you see the similarity that the INT data type  is
present. padding is done only when there is IMBALANCE in the structure of
the bytes arranged coz of different datatype).

your FIRST and SECOND e.g.
 has CHAR and INT both.. in first case, the character field add 3 more
bytes, where in your second e.g. the work is done only in the 4 byte.(as you
have noticed already)






struct node
> {
> char d;
> };
> int main(){
>    struct node a;
>    printf("size = %d",sizeof(a));
> }
>
>  Can someone please explain, how is the whole padding thing happening??
>
>
in 3rd e.g. you don't have any INT, so it DOESN'T need anything to pad for.
coz no BIGGER data type is present in the structure... it(padding) only
happens when we have different data types in the structure.. hope u'll get
the point.. :)


> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Thanks and Regards
VIKAS SINGH
MCA- final year
NIT DURGAPUR
email:
 vikas.singh1...@gmail.com
 shyguy1...@gmail.com
http://smrit.wordpress.com

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to