Re: [algogeeks] difference between the two

2011-08-07 Thread Puneet Gautam
How do u define size of compiler...?


On 8/6/11, SANDEEP CHUGH sandeep.aa...@gmail.com wrote:
 @ sid : can u please elaborate considering these parameters

 1 size of compiler
 2 size of os and
 3 size of processor

 please explain for any case, considering these three parameters and tell me
 how these parameters do affect..

 ty

 On Sat, Aug 6, 2011 at 9:53 PM, siddharth srivastava
 akssps...@gmail.comwrote:



 On 6 August 2011 21:50, siddharth srivastava akssps...@gmail.com wrote:

 Hi

 On 6 August 2011 20:20, SANDEEP CHUGH sandeep.aa...@gmail.com wrote:

 padding wud be between int  char..(for ur last case)

 now u said if it starts at 4 , still the block will be 8 (size of
 double), that is 7 bytes to be padded..

 so double element of structure would be starting at 4 + char(1) + 7 byte
 padding  ==12

 but 12 is not a multiple of 8..


 the whole concept is about the alignment and word size.
 If word size is 4( 32 bit m/c) then the memory allocation would start at
 a
 multiple of 4 and if it is 8 , then the memory allocation would start at
 a
 multiple of 8.
 Moreover, the explaination I gave above hold for 64 bit architecture.
 For 32 bit architecture a double is 8 bytes aligned

 just a correction:
 on gcc, double is 4 bytes aligned


 but the word size is 4 bytes, so for a structure like this

  struct a
 {
 double d;
 int i;
 }

 the size would be 12 (on 32 bit) and 16 (on 64 bit)


 the result are for linux(gcc) system



 so there is a problem??






 On Sat, Aug 6, 2011 at 8:06 PM, siddharth srivastava 
 akssps...@gmail.com wrote:

  but if the reference is not 0 .. if it wud have been 4 then for tht
 case tell me do we really need that 7 bytes??


 yes, because in any case the block would be of 8 bytes only
 so even if you start at 4, you would have only 7 bytes left in that
 location for the next variable(which in this case is a double of 8
 bytes, so
 the allocation would start at the 8th byte from the first location)

 Look at the following stack: X represents occupied memory, - represents
 the memory to be padded (in bytes)

 X - - - - - - -
 
  - - - -

 So, first char occupies 1 byte, then there are only 7 bytes left at
 that
 index, which are not enough for a double to be stored (got my point)
 Moreover, if you had
 struct a{
 char c;
 int i;
 double d;
 }

 then the size would have been 16

 as after allocating a char, there was enough space for an int to be
 stored. (just not sure if padding would be after int or between int and
 char)





 and yeah it is dependent on compiler size..
 if u compile this snippet

 struct demo
 {
char   c;
double  d;
int s;

 }


 in turbo c , its giving 11,, that means compiler nt doing padding at
 all in turbo c..

 On Sat, Aug 6, 2011 at 7:50 PM, siddharth srivastava 
 akssps...@gmail.com wrote:

 Hi Sandeep

 On 6 August 2011 19:16, SANDEEP CHUGH sandeep.aa...@gmail.comwrote:

 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 

Re: [algogeeks] difference between the two

2011-08-06 Thread Puneet Gautam
There is no difference between the two...

On 32 bit system, both structures need every address location where
int and pointer are stored to be a multiple of 4(highest size is 4)..

On 64 bit,
even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
structure variables, then p2 should start at address which is multiple
of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at 16
not 12 so that p2 starts at 8's multiple.

This is done by padding pointer by 4bytes in both I and II struct. declarations.


Hope i made it clear...!

Thanks.




On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
 this says int is always 4 bytes and pointer is 8 bytes on 64 bit compiler.

 so how does padding affect these structures because of the difference in
 size of int and pointer?


 I tried this program
 https://ideone.com/CRU6x#view_edit_box
 char always gets 4 bytes whenever it has int or double in the same struct
 irrrespctive of the order of the declaration of variables.
 I thought char should get size 8 when there is a double in the ame struct
 whereas it gets size 4 only.
 what is the problem here?

 On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com wrote:

 i dont understand the diff btw dem, could u plz elaborate?

 Shashank Jain
 IIIrd year
 Computer Engineering
 Delhi College of Engineering



 On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal kamakshi...@gmail.com
  wrote:

 in case of 64 bit,
 size of second structure will also be 16 not 8


 On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 I think voth are just same..


 On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:

 in case of 64 bit machine y doesn't padding happen in the 2nd
 structure?


 On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
 harry.rat...@gmail.comwrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
 but   in 64 machine that enforce to be size of 8 . where padding will
 take int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding
 in
 one structure

 hence 2nd structure is good for use

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


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




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 2nd Year
 @MNNIT ALLAHABAD*


  --
 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,
 Kamakshi
 kamakshi...@gmail.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.


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




 --
 Tushar Bindal
 Computer Engineering
 Delhi College of Engineering
 Mob: +919818442705
 E-Mail : tushicom...@gmail.com
 Website: www.jugadengg.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.



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

Re: [algogeeks] difference between the two

2011-08-06 Thread Puneet Gautam
Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..

But padding rule remains same for both structures as mentioned above...




On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
 There is no difference between the two...

 On 32 bit system, both structures need every address location where
 int and pointer are stored to be a multiple of 4(highest size is 4)..

 On 64 bit,
 even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
 structure variables, then p2 should start at address which is multiple
 of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at 16
 not 12 so that p2 starts at 8's multiple.

 This is done by padding pointer by 4bytes in both I and II struct.
 declarations.


 Hope i made it clear...!

 Thanks.




 On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
 this says int is always 4 bytes and pointer is 8 bytes on 64 bit
 compiler.

 so how does padding affect these structures because of the difference in
 size of int and pointer?


 I tried this program
 https://ideone.com/CRU6x#view_edit_box
 char always gets 4 bytes whenever it has int or double in the same struct
 irrrespctive of the order of the declaration of variables.
 I thought char should get size 8 when there is a double in the ame struct
 whereas it gets size 4 only.
 what is the problem here?

 On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com
 wrote:

 i dont understand the diff btw dem, could u plz elaborate?

 Shashank Jain
 IIIrd year
 Computer Engineering
 Delhi College of Engineering



 On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
 kamakshi...@gmail.com
  wrote:

 in case of 64 bit,
 size of second structure will also be 16 not 8


 On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 I think voth are just same..


 On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:

 in case of 64 bit machine y doesn't padding happen in the 2nd
 structure?


 On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
 harry.rat...@gmail.comwrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size
 ,
 but   in 64 machine that enforce to be size of 8 . where padding
 will
 take int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding
 in
 one structure

 hence 2nd structure is good for use

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


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




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 2nd Year
 @MNNIT ALLAHABAD*


  --
 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,
 Kamakshi
 kamakshi...@gmail.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.


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




 --
 Tushar Bindal
 Computer Engineering
 Delhi College of Engineering
 Mob: +919818442705
 E-Mail : tushicom...@gmail.com
 Website: www.jugadengg.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.




-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post 

Re: [algogeeks] difference between the two

2011-08-06 Thread Tushar Bindal
that means the order is immaterial.
the sizeof the struct always remains same irrespective of the order and just
depends on the type of variables???
why char with double does not get size in multiples of 8??

On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam puneet.nsi...@gmail.comwrote:

 Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..

 But padding rule remains same for both structures as mentioned above...




 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  There is no difference between the two...
 
  On 32 bit system, both structures need every address location where
  int and pointer are stored to be a multiple of 4(highest size is 4)..
 
  On 64 bit,
  even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
  structure variables, then p2 should start at address which is multiple
  of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at 16
  not 12 so that p2 starts at 8's multiple.
 
  This is done by padding pointer by 4bytes in both I and II struct.
  declarations.
 
 
  Hope i made it clear...!
 
  Thanks.
 
 
 
 
  On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
  this says int is always 4 bytes and pointer is 8 bytes on 64 bit
  compiler.
 
  so how does padding affect these structures because of the difference in
  size of int and pointer?
 
 
  I tried this program
  https://ideone.com/CRU6x#view_edit_box
  char always gets 4 bytes whenever it has int or double in the same
 struct
  irrrespctive of the order of the declaration of variables.
  I thought char should get size 8 when there is a double in the ame
 struct
  whereas it gets size 4 only.
  what is the problem here?
 
  On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com
  wrote:
 
  i dont understand the diff btw dem, could u plz elaborate?
 
  Shashank Jain
  IIIrd year
  Computer Engineering
  Delhi College of Engineering
 
 
 
  On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
  kamakshi...@gmail.com
   wrote:
 
  in case of 64 bit,
  size of second structure will also be 16 not 8
 
 
  On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
  usrivastav...@gmail.com wrote:
 
  I think voth are just same..
 
 
  On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:
 
  in case of 64 bit machine y doesn't padding happen in the 2nd
  structure?
 
 
  On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
  harry.rat...@gmail.comwrote:
 
  no ,if u r using 32 bit machine . that will use 4 byte pointer size
  ,
  but   in 64 machine that enforce to be size of 8 . where padding
  will
  take int your given first structure
 
  so for 32 bit- size will 8 8 for both structure
  for 64 bit - size will 16 and 12 respectively cause of 4 bit
 padding
  in
  one structure
 
  hence 2nd structure is good for use
 
  --
  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.
 
 
   --
  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.
 
 
 
 
  --
  *UTKARSH SRIVASTAV
  CSE-3
  B-Tech 2nd Year
  @MNNIT ALLAHABAD*
 
 
   --
  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,
  Kamakshi
  kamakshi...@gmail.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.
 
 
   --
  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.
 
 
 
 
  --
  Tushar Bindal
  Computer Engineering
  Delhi College of Engineering
  Mob: +919818442705
  E-Mail : tushicom...@gmail.com
  Website: www.jugadengg.com
 

Re: [algogeeks] difference between the two

2011-08-06 Thread Prashant Gupta
Interesting :
#includeiostream
using namespace std;
int main()
{
struct p{
int i;
char j;
char k;
};
struct q{
char j;
int i;
char k;
};
printf(p=%u q=%u,sizeof(p),sizeof(q));
return 0;
}
o/p : p=8 q=12

On Sat, Aug 6, 2011 at 2:55 PM, Tushar Bindal tushicom...@gmail.com wrote:

 that means the order is immaterial.
 the sizeof the struct always remains same irrespective of the order and
 just depends on the type of variables???
 why char with double does not get size in multiples of 8??


 On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam puneet.nsi...@gmail.comwrote:

 Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..

 But padding rule remains same for both structures as mentioned above...




 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  There is no difference between the two...
 
  On 32 bit system, both structures need every address location where
  int and pointer are stored to be a multiple of 4(highest size is 4)..
 
  On 64 bit,
  even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
  structure variables, then p2 should start at address which is multiple
  of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at 16
  not 12 so that p2 starts at 8's multiple.
 
  This is done by padding pointer by 4bytes in both I and II struct.
  declarations.
 
 
  Hope i made it clear...!
 
  Thanks.
 
 
 
 
  On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
  this says int is always 4 bytes and pointer is 8 bytes on 64 bit
  compiler.
 
  so how does padding affect these structures because of the difference
 in
  size of int and pointer?
 
 
  I tried this program
  https://ideone.com/CRU6x#view_edit_box
  char always gets 4 bytes whenever it has int or double in the same
 struct
  irrrespctive of the order of the declaration of variables.
  I thought char should get size 8 when there is a double in the ame
 struct
  whereas it gets size 4 only.
  what is the problem here?
 
  On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com
  wrote:
 
  i dont understand the diff btw dem, could u plz elaborate?
 
  Shashank Jain
  IIIrd year
  Computer Engineering
  Delhi College of Engineering
 
 
 
  On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
  kamakshi...@gmail.com
   wrote:
 
  in case of 64 bit,
  size of second structure will also be 16 not 8
 
 
  On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
  usrivastav...@gmail.com wrote:
 
  I think voth are just same..
 
 
  On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com
 wrote:
 
  in case of 64 bit machine y doesn't padding happen in the 2nd
  structure?
 
 
  On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
  harry.rat...@gmail.comwrote:
 
  no ,if u r using 32 bit machine . that will use 4 byte pointer
 size
  ,
  but   in 64 machine that enforce to be size of 8 . where padding
  will
  take int your given first structure
 
  so for 32 bit- size will 8 8 for both structure
  for 64 bit - size will 16 and 12 respectively cause of 4 bit
 padding
  in
  one structure
 
  hence 2nd structure is good for use
 
  --
  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.
 
 
   --
  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.
 
 
 
 
  --
  *UTKARSH SRIVASTAV
  CSE-3
  B-Tech 2nd Year
  @MNNIT ALLAHABAD*
 
 
   --
  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,
  Kamakshi
  kamakshi...@gmail.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.
 
 
   --
  You received this message because you are subscribed to the Google
  Groups
  Algorithm Geeks group.
  To post to this group, send email to 

Re: [algogeeks] difference between the two

2011-08-06 Thread Nitish Garg
I think that the order is important. Because when we consider an array of
structures the order becomes extremely important just as shown in the above
example.

On Sat, Aug 6, 2011 at 6:18 PM, Prashant Gupta prashantatn...@gmail.comwrote:

 Interesting :
 #includeiostream
 using namespace std;
 int main()
 {
 struct p{
 int i;
 char j;
 char k;
 };
 struct q{
 char j;
 int i;
 char k;
 };
 printf(p=%u q=%u,sizeof(p),sizeof(q));
 return 0;
 }
 o/p : p=8 q=12

 On Sat, Aug 6, 2011 at 2:55 PM, Tushar Bindal tushicom...@gmail.comwrote:

 that means the order is immaterial.
 the sizeof the struct always remains same irrespective of the order and
 just depends on the type of variables???
 why char with double does not get size in multiples of 8??


 On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam 
 puneet.nsi...@gmail.comwrote:

 Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..

 But padding rule remains same for both structures as mentioned above...




 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  There is no difference between the two...
 
  On 32 bit system, both structures need every address location where
  int and pointer are stored to be a multiple of 4(highest size is 4)..
 
  On 64 bit,
  even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
  structure variables, then p2 should start at address which is multiple
  of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at 16
  not 12 so that p2 starts at 8's multiple.
 
  This is done by padding pointer by 4bytes in both I and II struct.
  declarations.
 
 
  Hope i made it clear...!
 
  Thanks.
 
 
 
 
  On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
  this says int is always 4 bytes and pointer is 8 bytes on 64 bit
  compiler.
 
  so how does padding affect these structures because of the difference
 in
  size of int and pointer?
 
 
  I tried this program
  https://ideone.com/CRU6x#view_edit_box
  char always gets 4 bytes whenever it has int or double in the same
 struct
  irrrespctive of the order of the declaration of variables.
  I thought char should get size 8 when there is a double in the ame
 struct
  whereas it gets size 4 only.
  what is the problem here?
 
  On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com
  wrote:
 
  i dont understand the diff btw dem, could u plz elaborate?
 
  Shashank Jain
  IIIrd year
  Computer Engineering
  Delhi College of Engineering
 
 
 
  On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
  kamakshi...@gmail.com
   wrote:
 
  in case of 64 bit,
  size of second structure will also be 16 not 8
 
 
  On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
  usrivastav...@gmail.com wrote:
 
  I think voth are just same..
 
 
  On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com
 wrote:
 
  in case of 64 bit machine y doesn't padding happen in the 2nd
  structure?
 
 
  On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
  harry.rat...@gmail.comwrote:
 
  no ,if u r using 32 bit machine . that will use 4 byte pointer
 size
  ,
  but   in 64 machine that enforce to be size of 8 . where padding
  will
  take int your given first structure
 
  so for 32 bit- size will 8 8 for both structure
  for 64 bit - size will 16 and 12 respectively cause of 4 bit
 padding
  in
  one structure
 
  hence 2nd structure is good for use
 
  --
  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.
 
 
   --
  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.
 
 
 
 
  --
  *UTKARSH SRIVASTAV
  CSE-3
  B-Tech 2nd Year
  @MNNIT ALLAHABAD*
 
 
   --
  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,
  Kamakshi
  kamakshi...@gmail.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
  

Re: [algogeeks] difference between the two

2011-08-06 Thread Puneet Gautam
See guys.. the order is important but the size of whole structure
needs to be a multiple of its largest sized variable...
eg:

struct p
{
double data;
char a;
char b;
char c;
char d;
}t;


struct q
{char c;
char d;
double data;
char a;
char b;
}t1;

sizeof(t)=16
sizeof(t1)=24

This can be explained:Lets say address starts at 0

In q structure, c and d take one byte each so data starts at 3 but it
cant start at 3 (not its size multiple)...
so double data starts at 8, leaving all 3-7 positions padded to char c and d

double ends at 16 so char a and b occupy 17 and 18 addresses.

But if next structure variable starts, it wud have to start at 19
which is not 8's multiple..

So , char a and b are padded till address 23 and hence next structure
variable can start at 24..(8 * 3)

Hence t1's size =24, neither 19 nor 12...

Similarly, we can account for structure p's variable t..t=16
bytes(char a,b,c,d occupy 4bytes, get padded upto 7 and double then
starts at 8 upto 15, next variable starts at 16..)

Am i clear...???



On 8/6/11, Nitish Garg nitishgarg1...@gmail.com wrote:
 I think that the order is important. Because when we consider an array of
 structures the order becomes extremely important just as shown in the above
 example.

 On Sat, Aug 6, 2011 at 6:18 PM, Prashant Gupta
 prashantatn...@gmail.comwrote:

 Interesting :
 #includeiostream
 using namespace std;
 int main()
 {
 struct p{
 int i;
 char j;
 char k;
 };
 struct q{
 char j;
 int i;
 char k;
 };
 printf(p=%u q=%u,sizeof(p),sizeof(q));
 return 0;
 }
 o/p : p=8 q=12

 On Sat, Aug 6, 2011 at 2:55 PM, Tushar Bindal
 tushicom...@gmail.comwrote:

 that means the order is immaterial.
 the sizeof the struct always remains same irrespective of the order and
 just depends on the type of variables???
 why char with double does not get size in multiples of 8??


 On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam
 puneet.nsi...@gmail.comwrote:

 Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..

 But padding rule remains same for both structures as mentioned above...




 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  There is no difference between the two...
 
  On 32 bit system, both structures need every address location where
  int and pointer are stored to be a multiple of 4(highest size is 4)..
 
  On 64 bit,
  even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
  structure variables, then p2 should start at address which is multiple
  of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at 16
  not 12 so that p2 starts at 8's multiple.
 
  This is done by padding pointer by 4bytes in both I and II struct.
  declarations.
 
 
  Hope i made it clear...!
 
  Thanks.
 
 
 
 
  On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
  this says int is always 4 bytes and pointer is 8 bytes on 64 bit
  compiler.
 
  so how does padding affect these structures because of the difference
 in
  size of int and pointer?
 
 
  I tried this program
  https://ideone.com/CRU6x#view_edit_box
  char always gets 4 bytes whenever it has int or double in the same
 struct
  irrrespctive of the order of the declaration of variables.
  I thought char should get size 8 when there is a double in the ame
 struct
  whereas it gets size 4 only.
  what is the problem here?
 
  On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com
  wrote:
 
  i dont understand the diff btw dem, could u plz elaborate?
 
  Shashank Jain
  IIIrd year
  Computer Engineering
  Delhi College of Engineering
 
 
 
  On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
  kamakshi...@gmail.com
   wrote:
 
  in case of 64 bit,
  size of second structure will also be 16 not 8
 
 
  On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
  usrivastav...@gmail.com wrote:
 
  I think voth are just same..
 
 
  On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com
 wrote:
 
  in case of 64 bit machine y doesn't padding happen in the 2nd
  structure?
 
 
  On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
  harry.rat...@gmail.comwrote:
 
  no ,if u r using 32 bit machine . that will use 4 byte pointer
 size
  ,
  but   in 64 machine that enforce to be size of 8 . where padding
  will
  take int your given first structure
 
  so for 32 bit- size will 8 8 for both structure
  for 64 bit - size will 16 and 12 respectively cause of 4 bit
 padding
  in
  one structure
 
  hence 2nd structure is good for use
 
  --
  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, 

Re: [algogeeks] difference between the two

2011-08-06 Thread Puneet Gautam
Order is important ... but in the main case here which is

 1) struct list
{
   int data;
   list *next;
   }
and
2) struct list
{
   list *next;
   int data;
   }
order is not affecting its size...!!

On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
 See guys.. the order is important but the size of whole structure
 needs to be a multiple of its largest sized variable...
 eg:

 struct p
 {
 double data;
 char a;
 char b;
 char c;
 char d;
 }t;


 struct q
 {char c;
 char d;
 double data;
 char a;
 char b;
 }t1;

 sizeof(t)=16
 sizeof(t1)=24

 This can be explained:Lets say address starts at 0

 In q structure, c and d take one byte each so data starts at 3 but it
 cant start at 3 (not its size multiple)...
 so double data starts at 8, leaving all 3-7 positions padded to char c and
 d

 double ends at 16 so char a and b occupy 17 and 18 addresses.

 But if next structure variable starts, it wud have to start at 19
 which is not 8's multiple..

 So , char a and b are padded till address 23 and hence next structure
 variable can start at 24..(8 * 3)

 Hence t1's size =24, neither 19 nor 12...

 Similarly, we can account for structure p's variable t..t=16
 bytes(char a,b,c,d occupy 4bytes, get padded upto 7 and double then
 starts at 8 upto 15, next variable starts at 16..)

 Am i clear...???



 On 8/6/11, Nitish Garg nitishgarg1...@gmail.com wrote:
 I think that the order is important. Because when we consider an array of
 structures the order becomes extremely important just as shown in the
 above
 example.

 On Sat, Aug 6, 2011 at 6:18 PM, Prashant Gupta
 prashantatn...@gmail.comwrote:

 Interesting :
 #includeiostream
 using namespace std;
 int main()
 {
 struct p{
 int i;
 char j;
 char k;
 };
 struct q{
 char j;
 int i;
 char k;
 };
 printf(p=%u q=%u,sizeof(p),sizeof(q));
 return 0;
 }
 o/p : p=8 q=12

 On Sat, Aug 6, 2011 at 2:55 PM, Tushar Bindal
 tushicom...@gmail.comwrote:

 that means the order is immaterial.
 the sizeof the struct always remains same irrespective of the order and
 just depends on the type of variables???
 why char with double does not get size in multiples of 8??


 On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam
 puneet.nsi...@gmail.comwrote:

 Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..

 But padding rule remains same for both structures as mentioned
 above...




 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  There is no difference between the two...
 
  On 32 bit system, both structures need every address location where
  int and pointer are stored to be a multiple of 4(highest size is
  4)..
 
  On 64 bit,
  even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
  structure variables, then p2 should start at address which is
  multiple
  of 8 as int data is 8bytes. So, if p1 starts at 0, it should end at
  16
  not 12 so that p2 starts at 8's multiple.
 
  This is done by padding pointer by 4bytes in both I and II struct.
  declarations.
 
 
  Hope i made it clear...!
 
  Thanks.
 
 
 
 
  On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
  this says int is always 4 bytes and pointer is 8 bytes on 64 bit
  compiler.
 
  so how does padding affect these structures because of the
  difference
 in
  size of int and pointer?
 
 
  I tried this program
  https://ideone.com/CRU6x#view_edit_box
  char always gets 4 bytes whenever it has int or double in the same
 struct
  irrrespctive of the order of the declaration of variables.
  I thought char should get size 8 when there is a double in the ame
 struct
  whereas it gets size 4 only.
  what is the problem here?
 
  On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain
  shashan...@gmail.com
  wrote:
 
  i dont understand the diff btw dem, could u plz elaborate?
 
  Shashank Jain
  IIIrd year
  Computer Engineering
  Delhi College of Engineering
 
 
 
  On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
  kamakshi...@gmail.com
   wrote:
 
  in case of 64 bit,
  size of second structure will also be 16 not 8
 
 
  On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
  usrivastav...@gmail.com wrote:
 
  I think voth are just same..
 
 
  On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com
 wrote:
 
  in case of 64 bit machine y doesn't padding happen in the 2nd
  structure?
 
 
  On Fri, Aug 5, 2011 at 11:21 PM, hary rathor
  harry.rat...@gmail.comwrote:
 
  no ,if u r using 32 bit machine . that will use 4 byte pointer
 size
  ,
  but   in 64 machine that enforce to be size of 8 . where
  padding
  will
  take int your given first structure
 
  so for 32 bit- size will 8 8 for both structure
  for 64 bit - size will 16 and 12 respectively cause of 4 bit
 padding
  in
  one 

Re: [algogeeks] difference between the two

2011-08-06 Thread SANDEEP CHUGH
@ puneet :
tell me the case if u take the address to be starting from 4 not 0..

On Sat, Aug 6, 2011 at 6:55 PM, SANDEEP CHUGH sandeep.aa...@gmail.comwrote:

 @ puneet :  ryt !!  gud explanation.


 On Sat, Aug 6, 2011 at 6:53 PM, Puneet Gautam puneet.nsi...@gmail.comwrote:

 Order is important ... but in the main case here which is

  1) struct list
{
   int data;
   list *next;
   }
 and
 2) struct list
{
   list *next;
   int data;
   }
 order is not affecting its size...!!

 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  See guys.. the order is important but the size of whole structure
  needs to be a multiple of its largest sized variable...
  eg:
 
  struct p
  {
  double data;
  char a;
  char b;
  char c;
  char d;
  }t;
 
 
  struct q
  {char c;
  char d;
  double data;
  char a;
  char b;
  }t1;
 
  sizeof(t)=16
  sizeof(t1)=24
 
  This can be explained:Lets say address starts at 0
 
  In q structure, c and d take one byte each so data starts at 3 but it
  cant start at 3 (not its size multiple)...
  so double data starts at 8, leaving all 3-7 positions padded to char c
 and
  d
 
  double ends at 16 so char a and b occupy 17 and 18 addresses.
 
  But if next structure variable starts, it wud have to start at 19
  which is not 8's multiple..
 
  So , char a and b are padded till address 23 and hence next structure
  variable can start at 24..(8 * 3)
 
  Hence t1's size =24, neither 19 nor 12...
 
  Similarly, we can account for structure p's variable t..t=16
  bytes(char a,b,c,d occupy 4bytes, get padded upto 7 and double then
  starts at 8 upto 15, next variable starts at 16..)
 
  Am i clear...???
 
 
 
  On 8/6/11, Nitish Garg nitishgarg1...@gmail.com wrote:
  I think that the order is important. Because when we consider an array
 of
  structures the order becomes extremely important just as shown in the
  above
  example.
 
  On Sat, Aug 6, 2011 at 6:18 PM, Prashant Gupta
  prashantatn...@gmail.comwrote:
 
  Interesting :
  #includeiostream
  using namespace std;
  int main()
  {
  struct p{
  int i;
  char j;
  char k;
  };
  struct q{
  char j;
  int i;
  char k;
  };
  printf(p=%u q=%u,sizeof(p),sizeof(q));
  return 0;
  }
  o/p : p=8 q=12
 
  On Sat, Aug 6, 2011 at 2:55 PM, Tushar Bindal
  tushicom...@gmail.comwrote:
 
  that means the order is immaterial.
  the sizeof the struct always remains same irrespective of the order
 and
  just depends on the type of variables???
  why char with double does not get size in multiples of 8??
 
 
  On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam
  puneet.nsi...@gmail.comwrote:
 
  Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..
 
  But padding rule remains same for both structures as mentioned
  above...
 
 
 
 
  On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
   There is no difference between the two...
  
   On 32 bit system, both structures need every address location
 where
   int and pointer are stored to be a multiple of 4(highest size is
   4)..
  
   On 64 bit,
   even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
   structure variables, then p2 should start at address which is
   multiple
   of 8 as int data is 8bytes. So, if p1 starts at 0, it should end
 at
   16
   not 12 so that p2 starts at 8's multiple.
  
   This is done by padding pointer by 4bytes in both I and II struct.
   declarations.
  
  
   Hope i made it clear...!
  
   Thanks.
  
  
  
  
   On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
  
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
   this says int is always 4 bytes and pointer is 8 bytes on 64 bit
   compiler.
  
   so how does padding affect these structures because of the
   difference
  in
   size of int and pointer?
  
  
   I tried this program
   https://ideone.com/CRU6x#view_edit_box
   char always gets 4 bytes whenever it has int or double in the
 same
  struct
   irrrespctive of the order of the declaration of variables.
   I thought char should get size 8 when there is a double in the
 ame
  struct
   whereas it gets size 4 only.
   what is the problem here?
  
   On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain
   shashan...@gmail.com
   wrote:
  
   i dont understand the diff btw dem, could u plz elaborate?
  
   Shashank Jain
   IIIrd year
   Computer Engineering
   Delhi College of Engineering
  
  
  
   On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
   kamakshi...@gmail.com
wrote:
  
   in case of 64 bit,
   size of second structure will also be 16 not 8
  
  
   On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
   usrivastav...@gmail.com wrote:
  
   I think voth are just same..
  
  
   On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com
  wrote:
  
   in case of 64 bit 

Re: [algogeeks] difference between the two

2011-08-06 Thread Puneet Gautam
Well, even in dat case no difference occurs
As far as i know, because we cant predict where its address is going
to start from, in real time i.e. in memory, it will always give u the
same size as output..if u run the code..

So the whole point is that the size comes down to the highest sized
variable in the struct declaration and the order in which the
variables are declared...

The rerference 0 was only for explanation...!

Thanks..

On 8/6/11, SANDEEP CHUGH sandeep.aa...@gmail.com wrote:
 @ puneet :
 tell me the case if u take the address to be starting from 4 not 0..

 On Sat, Aug 6, 2011 at 6:55 PM, SANDEEP CHUGH
 sandeep.aa...@gmail.comwrote:

 @ puneet :  ryt !!  gud explanation.


 On Sat, Aug 6, 2011 at 6:53 PM, Puneet Gautam
 puneet.nsi...@gmail.comwrote:

 Order is important ... but in the main case here which is

  1) struct list
{
   int data;
   list *next;
   }
 and
 2) struct list
{
   list *next;
   int data;
   }
 order is not affecting its size...!!

 On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
  See guys.. the order is important but the size of whole structure
  needs to be a multiple of its largest sized variable...
  eg:
 
  struct p
  {
  double data;
  char a;
  char b;
  char c;
  char d;
  }t;
 
 
  struct q
  {char c;
  char d;
  double data;
  char a;
  char b;
  }t1;
 
  sizeof(t)=16
  sizeof(t1)=24
 
  This can be explained:Lets say address starts at 0
 
  In q structure, c and d take one byte each so data starts at 3 but it
  cant start at 3 (not its size multiple)...
  so double data starts at 8, leaving all 3-7 positions padded to char c
 and
  d
 
  double ends at 16 so char a and b occupy 17 and 18 addresses.
 
  But if next structure variable starts, it wud have to start at 19
  which is not 8's multiple..
 
  So , char a and b are padded till address 23 and hence next structure
  variable can start at 24..(8 * 3)
 
  Hence t1's size =24, neither 19 nor 12...
 
  Similarly, we can account for structure p's variable t..t=16
  bytes(char a,b,c,d occupy 4bytes, get padded upto 7 and double then
  starts at 8 upto 15, next variable starts at 16..)
 
  Am i clear...???
 
 
 
  On 8/6/11, Nitish Garg nitishgarg1...@gmail.com wrote:
  I think that the order is important. Because when we consider an array
 of
  structures the order becomes extremely important just as shown in the
  above
  example.
 
  On Sat, Aug 6, 2011 at 6:18 PM, Prashant Gupta
  prashantatn...@gmail.comwrote:
 
  Interesting :
  #includeiostream
  using namespace std;
  int main()
  {
  struct p{
  int i;
  char j;
  char k;
  };
  struct q{
  char j;
  int i;
  char k;
  };
  printf(p=%u q=%u,sizeof(p),sizeof(q));
  return 0;
  }
  o/p : p=8 q=12
 
  On Sat, Aug 6, 2011 at 2:55 PM, Tushar Bindal
  tushicom...@gmail.comwrote:
 
  that means the order is immaterial.
  the sizeof the struct always remains same irrespective of the order
 and
  just depends on the type of variables???
  why char with double does not get size in multiples of 8??
 
 
  On Sat, Aug 6, 2011 at 12:54 PM, Puneet Gautam
  puneet.nsi...@gmail.comwrote:
 
  Sorry guys, int is 4 bytes on 64 bit and 2 bytes on 32 bit system..
 
  But padding rule remains same for both structures as mentioned
  above...
 
 
 
 
  On 8/6/11, Puneet Gautam puneet.nsi...@gmail.com wrote:
   There is no difference between the two...
  
   On 32 bit system, both structures need every address location
 where
   int and pointer are stored to be a multiple of 4(highest size is
   4)..
  
   On 64 bit,
   even if pointer is 4bytes(say, in 64 bit system), and p1, p2 be
   structure variables, then p2 should start at address which is
   multiple
   of 8 as int data is 8bytes. So, if p1 starts at 0, it should end
 at
   16
   not 12 so that p2 starts at 8's multiple.
  
   This is done by padding pointer by 4bytes in both I and II
   struct.
   declarations.
  
  
   Hope i made it clear...!
  
   Thanks.
  
  
  
  
   On 8/6/11, Tushar Bindal tushicom...@gmail.com wrote:
  
 
 http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
   this says int is always 4 bytes and pointer is 8 bytes on 64 bit
   compiler.
  
   so how does padding affect these structures because of the
   difference
  in
   size of int and pointer?
  
  
   I tried this program
   https://ideone.com/CRU6x#view_edit_box
   char always gets 4 bytes whenever it has int or double in the
 same
  struct
   irrrespctive of the order of the declaration of variables.
   I thought char should get size 8 when there is a double in the
 ame
  struct
   whereas it gets size 4 only.
   what is the problem here?
  
   On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain
   shashan...@gmail.com
   wrote:
  
   i dont understand the diff btw dem, could u plz elaborate?
  
   

Re: [algogeeks] difference between the two

2011-08-06 Thread SANDEEP CHUGH
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)..

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.



Re: [algogeeks] difference between the two

2011-08-06 Thread siddharth srivastava
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.



Re: [algogeeks] difference between the two

2011-08-06 Thread SANDEEP CHUGH
@sid : yeah agree to ur explanation..
 but if the reference is not 0 .. if it wud have been 4 then for tht case
tell me do we really need that 7 bytes??

and yeah it is dependent on compiler size..
if u compile this snippet

struct demo
{
   char   c;
   double  d;
   int s;

 }


in turbo c , its giving 11,, that means compiler nt doing padding at all in
turbo c..

On Sat, Aug 6, 2011 at 7:50 PM, siddharth srivastava akssps...@gmail.comwrote:

 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.


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



Re: [algogeeks] difference between the two

2011-08-06 Thread siddharth srivastava

  but if the reference is not 0 .. if it wud have been 4 then for tht case
 tell me do we really need that 7 bytes??


yes, because in any case the block would be of 8 bytes only
so even if you start at 4, you would have only 7 bytes left in that location
for the next variable(which in this case is a double of 8 bytes, so the
allocation would start at the 8th byte from the first location)

Look at the following stack: X represents occupied memory, - represents the
memory to be padded (in bytes)

X - - - - - - -

 - - - -

So, first char occupies 1 byte, then there are only 7 bytes left at that
index, which are not enough for a double to be stored (got my point)
Moreover, if you had
struct a{
char c;
int i;
double d;
}

then the size would have been 16

as after allocating a char, there was enough space for an int to be stored.
(just not sure if padding would be after int or between int and char)





 and yeah it is dependent on compiler size..
 if u compile this snippet

 struct demo
 {
char   c;
double  d;
int s;

 }


 in turbo c , its giving 11,, that means compiler nt doing padding at all in
 turbo c..

 On Sat, Aug 6, 2011 at 7:50 PM, siddharth srivastava 
 akssps...@gmail.comwrote:

 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.


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



Re: [algogeeks] difference between the two

2011-08-06 Thread siddharth srivastava
Hi

On 6 August 2011 20:20, SANDEEP CHUGH sandeep.aa...@gmail.com wrote:

 padding wud be between int  char..(for ur last case)

 now u said if it starts at 4 , still the block will be 8 (size of double),
 that is 7 bytes to be padded..

 so double element of structure would be starting at 4 + char(1) + 7 byte
 padding  ==12

 but 12 is not a multiple of 8..


the whole concept is about the alignment and word size.
If word size is 4( 32 bit m/c) then the memory allocation would start at a
multiple of 4 and if it is 8 , then the memory allocation would start at a
multiple of 8.
Moreover, the explaination I gave above hold for 64 bit architecture.
For 32 bit architecture a double is 8 bytes aligned but the word size is 4
bytes, so for a structure like this

struct a
{
double d;
int i;
}

the size would be 12 (on 32 bit) and 16 (on 64 bit)


 so there is a problem??






 On Sat, Aug 6, 2011 at 8:06 PM, siddharth srivastava 
 akssps...@gmail.comwrote:

  but if the reference is not 0 .. if it wud have been 4 then for tht case
 tell me do we really need that 7 bytes??


 yes, because in any case the block would be of 8 bytes only
 so even if you start at 4, you would have only 7 bytes left in that
 location for the next variable(which in this case is a double of 8 bytes, so
 the allocation would start at the 8th byte from the first location)

 Look at the following stack: X represents occupied memory, - represents
 the memory to be padded (in bytes)

 X - - - - - - -
 
  - - - -

 So, first char occupies 1 byte, then there are only 7 bytes left at that
 index, which are not enough for a double to be stored (got my point)
 Moreover, if you had
 struct a{
 char c;
 int i;
 double d;
 }

 then the size would have been 16

 as after allocating a char, there was enough space for an int to be
 stored. (just not sure if padding would be after int or between int and
 char)





 and yeah it is dependent on compiler size..
 if u compile this snippet

 struct demo
 {
char   c;
double  d;
int s;

 }


 in turbo c , its giving 11,, that means compiler nt doing padding at all
 in turbo c..

 On Sat, Aug 6, 2011 at 7:50 PM, siddharth srivastava 
 akssps...@gmail.com wrote:

 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.


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

Re: [algogeeks] difference between the two

2011-08-06 Thread siddharth srivastava
On 6 August 2011 21:50, siddharth srivastava akssps...@gmail.com wrote:

 Hi

 On 6 August 2011 20:20, SANDEEP CHUGH sandeep.aa...@gmail.com wrote:

 padding wud be between int  char..(for ur last case)

 now u said if it starts at 4 , still the block will be 8 (size of double),
 that is 7 bytes to be padded..

 so double element of structure would be starting at 4 + char(1) + 7 byte
 padding  ==12

 but 12 is not a multiple of 8..


 the whole concept is about the alignment and word size.
 If word size is 4( 32 bit m/c) then the memory allocation would start at a
 multiple of 4 and if it is 8 , then the memory allocation would start at a
 multiple of 8.
 Moreover, the explaination I gave above hold for 64 bit architecture.
 For 32 bit architecture a double is 8 bytes aligned

just a correction:
on gcc, double is 4 bytes aligned


 but the word size is 4 bytes, so for a structure like this

  struct a
 {
 double d;
 int i;
 }

 the size would be 12 (on 32 bit) and 16 (on 64 bit)


the result are for linux(gcc) system



 so there is a problem??






 On Sat, Aug 6, 2011 at 8:06 PM, siddharth srivastava akssps...@gmail.com
  wrote:

  but if the reference is not 0 .. if it wud have been 4 then for tht case
 tell me do we really need that 7 bytes??


 yes, because in any case the block would be of 8 bytes only
 so even if you start at 4, you would have only 7 bytes left in that
 location for the next variable(which in this case is a double of 8 bytes, so
 the allocation would start at the 8th byte from the first location)

 Look at the following stack: X represents occupied memory, - represents
 the memory to be padded (in bytes)

 X - - - - - - -
 
  - - - -

 So, first char occupies 1 byte, then there are only 7 bytes left at that
 index, which are not enough for a double to be stored (got my point)
 Moreover, if you had
 struct a{
 char c;
 int i;
 double d;
 }

 then the size would have been 16

 as after allocating a char, there was enough space for an int to be
 stored. (just not sure if padding would be after int or between int and
 char)





 and yeah it is dependent on compiler size..
 if u compile this snippet

 struct demo
 {
char   c;
double  d;
int s;

 }


 in turbo c , its giving 11,, that means compiler nt doing padding at all
 in turbo c..

 On Sat, Aug 6, 2011 at 7:50 PM, siddharth srivastava 
 akssps...@gmail.com wrote:

 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.


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

Re: [algogeeks] difference between the two

2011-08-06 Thread SANDEEP CHUGH
@ sid : can u please elaborate considering these parameters

1 size of compiler
2 size of os and
3 size of processor

please explain for any case, considering these three parameters and tell me
how these parameters do affect..

ty

On Sat, Aug 6, 2011 at 9:53 PM, siddharth srivastava akssps...@gmail.comwrote:



 On 6 August 2011 21:50, siddharth srivastava akssps...@gmail.com wrote:

 Hi

 On 6 August 2011 20:20, SANDEEP CHUGH sandeep.aa...@gmail.com wrote:

 padding wud be between int  char..(for ur last case)

 now u said if it starts at 4 , still the block will be 8 (size of
 double), that is 7 bytes to be padded..

 so double element of structure would be starting at 4 + char(1) + 7 byte
 padding  ==12

 but 12 is not a multiple of 8..


 the whole concept is about the alignment and word size.
 If word size is 4( 32 bit m/c) then the memory allocation would start at a
 multiple of 4 and if it is 8 , then the memory allocation would start at a
 multiple of 8.
 Moreover, the explaination I gave above hold for 64 bit architecture.
 For 32 bit architecture a double is 8 bytes aligned

 just a correction:
 on gcc, double is 4 bytes aligned


 but the word size is 4 bytes, so for a structure like this

  struct a
 {
 double d;
 int i;
 }

 the size would be 12 (on 32 bit) and 16 (on 64 bit)


 the result are for linux(gcc) system



 so there is a problem??






 On Sat, Aug 6, 2011 at 8:06 PM, siddharth srivastava 
 akssps...@gmail.com wrote:

  but if the reference is not 0 .. if it wud have been 4 then for tht
 case tell me do we really need that 7 bytes??


 yes, because in any case the block would be of 8 bytes only
 so even if you start at 4, you would have only 7 bytes left in that
 location for the next variable(which in this case is a double of 8 bytes, 
 so
 the allocation would start at the 8th byte from the first location)

 Look at the following stack: X represents occupied memory, - represents
 the memory to be padded (in bytes)

 X - - - - - - -
 
  - - - -

 So, first char occupies 1 byte, then there are only 7 bytes left at that
 index, which are not enough for a double to be stored (got my point)
 Moreover, if you had
 struct a{
 char c;
 int i;
 double d;
 }

 then the size would have been 16

 as after allocating a char, there was enough space for an int to be
 stored. (just not sure if padding would be after int or between int and
 char)





 and yeah it is dependent on compiler size..
 if u compile this snippet

 struct demo
 {
char   c;
double  d;
int s;

 }


 in turbo c , its giving 11,, that means compiler nt doing padding at
 all in turbo c..

 On Sat, Aug 6, 2011 at 7:50 PM, siddharth srivastava 
 akssps...@gmail.com wrote:

 Hi Sandeep

 On 6 August 2011 19:16, SANDEEP CHUGH sandeep.aa...@gmail.comwrote:

 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.


  --
 You received this 

Re: [algogeeks] difference between the two

2011-08-05 Thread hary rathor
no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
but   in 64 machine that enforce to be size of 8 . where padding will take
int your given first structure

so for 32 bit- size will 8 8 for both structure
for 64 bit - size will 16 and 12 respectively cause of 4 bit padding in one
structure

hence 2nd structure is good for use

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



Re: [algogeeks] difference between the two

2011-08-05 Thread priya v
in case of 64 bit machine y doesn't padding happen in the 2nd structure?

On Fri, Aug 5, 2011 at 11:21 PM, hary rathor harry.rat...@gmail.com wrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
 but   in 64 machine that enforce to be size of 8 . where padding will take
 int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding in one
 structure

 hence 2nd structure is good for use

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


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



Re: [algogeeks] difference between the two

2011-08-05 Thread UTKARSH SRIVASTAV
I think voth are just same..

On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:

 in case of 64 bit machine y doesn't padding happen in the 2nd structure?


 On Fri, Aug 5, 2011 at 11:21 PM, hary rathor harry.rat...@gmail.comwrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
 but   in 64 machine that enforce to be size of 8 . where padding will take
 int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding in
 one structure

 hence 2nd structure is good for use

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


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




-- 
*UTKARSH SRIVASTAV
CSE-3
B-Tech 2nd Year
@MNNIT ALLAHABAD*

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



Re: [algogeeks] difference between the two

2011-08-05 Thread Kamakshii Aggarwal
in case of 64 bit,
size of second structure will also be 16 not 8

On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV
usrivastav...@gmail.comwrote:

 I think voth are just same..


 On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:

 in case of 64 bit machine y doesn't padding happen in the 2nd structure?


 On Fri, Aug 5, 2011 at 11:21 PM, hary rathor harry.rat...@gmail.comwrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
 but   in 64 machine that enforce to be size of 8 . where padding will
 take int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding in
 one structure

 hence 2nd structure is good for use

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


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




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 2nd Year
 @MNNIT ALLAHABAD*


  --
 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,
Kamakshi
kamakshi...@gmail.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.



Re: [algogeeks] difference between the two

2011-08-05 Thread Shashank Jain
i dont understand the diff btw dem, could u plz elaborate?

Shashank Jain
IIIrd year
Computer Engineering
Delhi College of Engineering



On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal
kamakshi...@gmail.comwrote:

 in case of 64 bit,
 size of second structure will also be 16 not 8


 On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 I think voth are just same..


 On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:

 in case of 64 bit machine y doesn't padding happen in the 2nd structure?


 On Fri, Aug 5, 2011 at 11:21 PM, hary rathor harry.rat...@gmail.comwrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
 but   in 64 machine that enforce to be size of 8 . where padding will
 take int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding in
 one structure

 hence 2nd structure is good for use

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


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




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 2nd Year
 @MNNIT ALLAHABAD*


  --
 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,
 Kamakshi
 kamakshi...@gmail.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.


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



Re: [algogeeks] difference between the two

2011-08-05 Thread Tushar Bindal
http://www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/xlf/html/xlfug/ug35.htm
this says int is always 4 bytes and pointer is 8 bytes on 64 bit compiler.

so how does padding affect these structures because of the difference in
size of int and pointer?


I tried this program
https://ideone.com/CRU6x#view_edit_box
char always gets 4 bytes whenever it has int or double in the same struct
irrrespctive of the order of the declaration of variables.
I thought char should get size 8 when there is a double in the ame struct
whereas it gets size 4 only.
what is the problem here?

On Sat, Aug 6, 2011 at 4:40 AM, Shashank Jain shashan...@gmail.com wrote:

 i dont understand the diff btw dem, could u plz elaborate?

 Shashank Jain
 IIIrd year
 Computer Engineering
 Delhi College of Engineering



 On Sat, Aug 6, 2011 at 12:32 AM, Kamakshii Aggarwal kamakshi...@gmail.com
  wrote:

 in case of 64 bit,
 size of second structure will also be 16 not 8


 On Fri, Aug 5, 2011 at 11:40 PM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 I think voth are just same..


 On Fri, Aug 5, 2011 at 10:57 AM, priya v pria@gmail.com wrote:

 in case of 64 bit machine y doesn't padding happen in the 2nd structure?


 On Fri, Aug 5, 2011 at 11:21 PM, hary rathor harry.rat...@gmail.comwrote:

 no ,if u r using 32 bit machine . that will use 4 byte pointer size ,
 but   in 64 machine that enforce to be size of 8 . where padding will
 take int your given first structure

 so for 32 bit- size will 8 8 for both structure
 for 64 bit - size will 16 and 12 respectively cause of 4 bit padding in
 one structure

 hence 2nd structure is good for use

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


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




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 2nd Year
 @MNNIT ALLAHABAD*


  --
 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,
 Kamakshi
 kamakshi...@gmail.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.


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




-- 
Tushar Bindal
Computer Engineering
Delhi College of Engineering
Mob: +919818442705
E-Mail : tushicom...@gmail.com
Website: www.jugadengg.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.