Hi Sandeep

On 6 August 2011 19:16, SANDEEP CHUGH <sandeep.aa...@gmail.com> wrote:

> take this case
>
> struct demo
> {
>    char       c;
>    double      d;
>    int         s;
>
>> }
>
>
> what wud be the size??
>
>
> solution is 24 according to following:-->
>
> char (1) + 7 byte padding +double(8)+int(4)+ 4 byte padding
>
>
> suppose address starts at 4..
>
>  i just wanna ask .. why there is 7 byte padding.. because just after 3
> bytes padding after char we are getting address that is multiple of 8(size
> of largest)..
>


after 3 bytes of padding i.e. the 4th byte(if reference is 0), is not a
multiple of 8.
Moreover, try understanding with this example:
visualize a stack of memory with each location having the size of the
largest sized variable in the structure.
So in your case, each stack element is ought to be 8 bytes (due to double)
Now, when first character is allocated, we have only 7 bytes left at that
location in the stack, hence double has to be allocated in the next
location.i.e. 2nd stack element and 8th byte (again reference 0)

So total size is ought to be occupied is 24 in this case


say if you had this declaration:
struct a
{
char a;
double d;
int c;
int e;
}

then too size would have been 24 as after allocation of int c, we still have
4 bytes in the same location which are then occupied by e (and were padded
in previous case)

Let me know, if I am not clear.

@All
Is it really architecture dependent (32 bit or 64 bit) ?



>
> can u please tell me??
>
>
>
> --
> 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.
>



-- 
Regards
Siddharth Srivastava

-- 
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