As explained by someone above : Structure padding is done to try and make
sure that variables start in memory at addresses that are a multiple of
their size. This is more efficient at hardware level (needs less cpu ticks
to read or write variables) and in some platforms this is mandatory,
though not on i386. There are CPU's that can only handle double
precision floats if they are aligned on addresses that are a multiople of 8.

On Sat, Jul 30, 2011 at 8:23 PM, tech rascal <[email protected]>wrote:

> @ Nikhil: please explain y these memory spaces r left empty??? or hw
> structure padding increase execution speed?
>
> On Sat, Jul 30, 2011 at 8:05 PM, Nikhil Gupta 
> <[email protected]>wrote:
>
>> They remain empty. You cannot use them. The purpose for which they are
>> left will be defeated then.
>>
>>
>> On Sat, Jul 30, 2011 at 1:03 PM, Puneet Gautam 
>> <[email protected]>wrote:
>>
>>> @everyone: What happens to those padded byte addresses.. do they
>>> remain empty or what..?
>>> Can we utilize those padded bytes in any way ..?
>>>
>>>
>>> On 7/30/11, tech rascal <[email protected]> wrote:
>>> > can anyone explain in detail .....how structure padding is
>>> advantageous???
>>> >
>>> > On Fri, Jul 29, 2011 at 10:28 PM, Rohit Srivastava
>>> > <[email protected]>wrote:
>>> >
>>> >> padding based on the bit interleaving(low order) which is basically
>>> >> hardware dependent.
>>> >>
>>> >>
>>> >> On Fri, Jul 29, 2011 at 10:10 PM, Puneet Gautam
>>> >> <[email protected]>wrote:
>>> >>
>>> >>> Thanks guys...so much...!!
>>> >>>
>>> >>> On 7/29/11, nullpointer <[email protected]> wrote:
>>> >>> > #include<stdio.h>
>>> >>> > #include<conio.h>
>>> >>> > struc MyStructA {
>>> >>> >
>>> >>> >  char a;
>>> >>> >  char b;
>>> >>> >  int c;
>>> >>> > };
>>> >>> >
>>> >>> > struct MyStructB {
>>> >>> >  char a;
>>> >>> >  int c;
>>> >>> >  char b;
>>> >>> > };
>>> >>> >
>>> >>> > int main(void) {
>>> >>> >
>>> >>> >  struct MyStructA A;
>>> >>> >  struct MyStructB B;
>>> >>> >
>>> >>> > int sizeA = sizeof(struct MyStructA);
>>> >>> >  int sizeB = sizeof(struct MyStructB);
>>> >>> >  return 0;
>>> >>> >
>>> >>> > }
>>> >>> >
>>> >>> > OUTPUT IS
>>> >>> > A = 8
>>> >>> > B = 12
>>> >>> > Structure padding is done to try and make sure that variables start
>>> in
>>> >>> > memory at addresses that are a multiple of their size.
>>> >>> > This is more efficient at hardware level (needs less cpu ticks to
>>> read
>>> >>> > or write variables) and in some platforms this is mandatory, though
>>> >>> > not on i386. There are CPU's that can only handle double precision
>>> >>> > floats if they are aligned on addresses that are a multiople of 8.
>>> >>> >
>>> >>> >
>>> >>> > In this struct:
>>> >>> > struct MyStructA {
>>> >>> >
>>> >>> >  char a;
>>> >>> >  char b;
>>> >>> > int c;
>>> >>> > };
>>> >>> > the beginning of the struct is to be assumed at 0 (I'l explain
>>> later)
>>> >>> > a is 1 byte so it needs no padding in front of it.
>>> >>> > the same goes for b.
>>> >>> > but c is 4 bytes. it should be placed at an address boundary that
>>> is a
>>> >>> > multiple of 4, so the compiler adds 2 dummy bytes in front of it.
>>> >>> > These 2 bytes change the size from 6 to 8.
>>> >>> > Now in this struct:
>>> >>> > struct MyStructB {
>>> >>> >  char a;
>>> >>> >  int c;
>>> >>> >  char b;
>>> >>> > };
>>> >>> >
>>> >>> > a starts on 0, so no need for padding.
>>> >>> > c needs 3 bytes in front of it, and b needs no padding.
>>> >>> > this would bring the struct size to 9. HOWEVER, suppose that you
>>> place
>>> >>> > 2 of those structs in an array, the address rules for the second
>>> >>> > struct in the array are the same as for the first struct.
>>> >>> > If that second struct would start at byte 10, this would not be
>>> true
>>> >>> > so the compiler also inserts some padding at the end of the
>>> structure
>>> >>> > so that the next struct after it starts at a multiple of the size
>>> of
>>> >>> > its largest member.
>>> >>> >
>>> >>> >
>>> >>> >
>>> >>> >
>>> >>> > On Jul 29, 3:36 pm, Arun Vishwanathan <[email protected]>
>>> wrote:
>>> >>> >> @puneet : no , in this case since 4 bytes will be used for int a
>>> and
>>> >>> int c
>>> >>> >> and then 1 byte for char b with 3 padded bytes next..it wud be the
>>> >>> >> same
>>> >>> >> here
>>> >>> >>
>>> >>> >> On Fri, Jul 29, 2011 at 12:11 PM, Puneet Gautam
>>> >>> >> <[email protected]>wrote:
>>> >>> >>
>>> >>> >>
>>> >>> >>
>>> >>> >> > @nikhil: If i declare "Char b" after "int c".., would there be
>>> any
>>> >>> >> > difference...?
>>> >>> >>
>>> >>> >> > On 7/28/11, Nikhil Gupta <[email protected]> wrote:
>>> >>> >> > > Here's another example.
>>> >>> >>
>>> >>> >> > > struct example
>>> >>> >> > > {
>>> >>> >> > > int a;
>>> >>> >> > > char b;
>>> >>> >> > > int c;
>>> >>> >> > > }
>>> >>> >>
>>> >>> >> > > Now if a variable of type example is declared then
>>> >>> >> > > (considering base address as 2000)
>>> >>> >> > > a gets : 2000 to 2003
>>> >>> >> > > b gets : 2004
>>> >>> >> > > c gets : 2005 to 2008 ? NO
>>> >>> >>
>>> >>> >> > > It gets 2008 to 2011. The bytes from 2005 to 2007 (3 bytes)
>>> are
>>> >>> padded
>>> >>> >> > > in
>>> >>> >> > > this case.
>>> >>> >>
>>> >>> >> > > On Thu, Jul 28, 2011 at 12:18 AM, Aman Goyal <
>>> >>> [email protected]>
>>> >>> >> > wrote:
>>> >>> >>
>>> >>> >> > >> yes this will be the case.
>>> >>> >>
>>> >>> >> > >> On Wed, Jul 27, 2011 at 11:35 PM, Puneet Gautam
>>> >>> >> > >> <[email protected]>wrote:
>>> >>> >>
>>> >>> >> > >>> @nikhil:So what u mean is that if i have:
>>> >>> >>
>>> >>> >> > >>> struct{
>>> >>> >> > >>> int a;
>>> >>> >> > >>> char b[5];
>>> >>> >> > >>> };
>>> >>> >>
>>> >>> >> > >>> the size of this struct's node will be 12 not 9.., to make
>>> it a
>>> >>> >> > multiple
>>> >>> >> > >>> of 4??
>>> >>> >>
>>> >>> >> > >>> On 7/26/11, Nikhil Gupta <[email protected]> wrote:
>>> >>> >> > >>> > Padding is not a topic of self referential structure.
>>> >>> >>
>>> >>> >> > >>> > Padding means that extra spaces of memory are used by the
>>> >>> compiler
>>> >>> >> > >>> > to
>>> >>> >> > >>> > allocate memory. This is done to have the memory address
>>> as a
>>> >>> >> > multiple
>>> >>> >> > >>> of
>>> >>> >> > >>> > the size of the variable. This speeds up the processing of
>>> >>> these
>>> >>> >> > >>> variables
>>> >>> >> > >>> > by the compiler.
>>> >>> >>
>>> >>> >> > >>> > On Tue, Jul 26, 2011 at 8:09 PM, Puneet Gautam
>>> >>> >> > >>> > <[email protected]>wrote:
>>> >>> >>
>>> >>> >> > >>> >> what is meant by padding in self_referenced structure?
>>> >>> >> > >>> >> Is it always necessary?
>>> >>> >>
>>> >>> >> > >>> >> --
>>> >>> >> > >>> >> You received this message because you are subscribed to
>>> the
>>> >>> >> > >>> >> Google
>>> >>> >> > >>> Groups
>>> >>> >> > >>> >> "Algorithm Geeks" group.
>>> >>> >> > >>> >> To post to this group, send email to
>>> >>> [email protected].
>>> >>> >> > >>> >> To unsubscribe from this group, send email to
>>> >>> >> > >>> >> [email protected].
>>> >>> >> > >>> >> For more options, visit this group at
>>> >>> >> > >>> >>http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >>
>>> >>> >> > >>> > --
>>> >>> >> > >>> > Nikhil Gupta
>>> >>> >> > >>> > Senior Co-ordinator, Publicity
>>> >>> >> > >>> > CSI, NSIT Students' Branch
>>> >>> >> > >>> > NSIT, New Delhi, India
>>> >>> >>
>>> >>> >> > >>> > --
>>> >>> >> > >>> > You received this message because you are subscribed to
>>> the
>>> >>> Google
>>> >>> >> > >>> Groups
>>> >>> >> > >>> > "Algorithm Geeks" group.
>>> >>> >> > >>> > To post to this group, send email to
>>> >>> [email protected].
>>> >>> >> > >>> > To unsubscribe from this group, send email to
>>> >>> >> > >>> > [email protected].
>>> >>> >> > >>> > For more options, visit this group at
>>> >>> >> > >>> >http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >>
>>> >>> >> > >>> --
>>> >>> >> > >>> You received this message because you are subscribed to the
>>> >>> Google
>>> >>> >> > Groups
>>> >>> >> > >>> "Algorithm Geeks" group.
>>> >>> >> > >>> To post to this group, send email to
>>> [email protected].
>>> >>> >> > >>> To unsubscribe from this group, send email to
>>> >>> >> > >>> [email protected].
>>> >>> >> > >>> For more options, visit this group at
>>> >>> >> > >>>http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >>
>>> >>> >> > >>  --
>>> >>> >> > >> You received this message because you are subscribed to the
>>> >>> >> > >> Google
>>> >>> >> > Groups
>>> >>> >> > >> "Algorithm Geeks" group.
>>> >>> >> > >> To post to this group, send email to
>>> [email protected].
>>> >>> >> > >> To unsubscribe from this group, send email to
>>> >>> >> > >> [email protected].
>>> >>> >> > >> For more options, visit this group at
>>> >>> >> > >>http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >>
>>> >>> >> > > --
>>> >>> >> > > Nikhil Gupta
>>> >>> >> > > Senior Co-ordinator, Publicity
>>> >>> >> > > CSI, NSIT Students' Branch
>>> >>> >> > > NSIT, New Delhi, India
>>> >>> >>
>>> >>> >> > > --
>>> >>> >> > > You received this message because you are subscribed to the
>>> Google
>>> >>> >> > > Groups
>>> >>> >> > > "Algorithm Geeks" group.
>>> >>> >> > > To post to this group, send email to
>>> [email protected].
>>> >>> >> > > To unsubscribe from this group, send email to
>>> >>> >> > > [email protected].
>>> >>> >> > > For more options, visit this group at
>>> >>> >> > >http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >>
>>> >>> >> > --
>>> >>> >> >  You received this message because you are subscribed to the
>>> Google
>>> >>> >> > Groups
>>> >>> >> > "Algorithm Geeks" group.
>>> >>> >> > To post to this group, send email to [email protected]
>>> .
>>> >>> >> > To unsubscribe from this group, send email to
>>> >>> >> > [email protected].
>>> >>> >> > For more options, visit this group at
>>> >>> >> >http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >>
>>> >>> >> --
>>> >>> >>  Arun Vish
>>> >>> >> Graduate Student
>>> >>> >> Department of Computer Science
>>> >>> >> University of Southern California
>>> >>> >
>>> >>> > --
>>> >>> > You received this message because you are subscribed to the Google
>>> >>> Groups
>>> >>> > "Algorithm Geeks" group.
>>> >>> > To post to this group, send email to [email protected].
>>> >>> > To unsubscribe from this group, send email to
>>> >>> > [email protected].
>>> >>> > For more options, visit this group at
>>> >>> > http://groups.google.com/group/algogeeks?hl=en.
>>> >>> >
>>> >>> >
>>> >>>
>>> >>> --
>>> >>> You received this message because you are subscribed to the Google
>>> Groups
>>> >>> "Algorithm Geeks" group.
>>> >>> To post to this group, send email to [email protected].
>>> >>> To unsubscribe from this group, send email to
>>> >>> [email protected].
>>> >>> For more options, visit this group at
>>> >>> http://groups.google.com/group/algogeeks?hl=en.
>>> >>>
>>> >>>
>>> >>  --
>>> >> You received this message because you are subscribed to the Google
>>> Groups
>>> >> "Algorithm Geeks" group.
>>> >> To post to this group, send email to [email protected].
>>> >> To unsubscribe from this group, send email to
>>> >> [email protected].
>>> >> For more options, visit this group at
>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>> >>
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> Groups
>>> > "Algorithm Geeks" group.
>>> > To post to this group, send email to [email protected].
>>> > To unsubscribe from this group, send email to
>>> > [email protected].
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/algogeeks?hl=en.
>>> >
>>> >
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>
>>
>> --
>> Nikhil Gupta
>> Senior Co-ordinator, Publicity
>> CSI, NSIT Students' Branch
>> NSIT, New Delhi, India
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Nikhil Gupta
Senior Co-ordinator, Publicity
CSI, NSIT Students' Branch
NSIT, New Delhi, India

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

Reply via email to