Help needed with: sizeof (struct a)

Dave Harris Dave.Harris at f14.n15.z1.fidonet.org
Sun Jun 23 00:40:55 AEST 1991


In a message of <Jun 20 21:49>, Dave Corcoran (1:114/15) writes: 

 >My Sun cc prints 4 for both structs, prints 3 for the array

 >-----------------8<-------------------- 
 >struct a {char x;char y;char z};
 >struct b {char x[3]};
 >char c[3];
 >main()
 >{
 >        printf("%d\n",sizeof (struct a));
 >        printf("%d\n",sizeof (struct b));
 >        printf("%d\n",sizeof c);
 >}
 >-----------------8<--------------------

 >Is there any way to cause cc to force sizeof return the actual size of 
 >the
 >structs without rounding up to the next highest sizeof (short)?
 >Thanx

I assume that you are getting 4 when expecting 3?
If so...

Normally, 4 IS the size of this struct when it is stored in memory.  Why you 
would want 3 returned escapes me.  You use the 3 to malloc() (or any number of 
other address calculations and), you are just asking for bugs.  

There might be some options that allow you to specify alignment on a byte 
boundary as opposed to a word boundary.  This will cost you some speed 
however.

Dave Harris 


 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!15!14!Dave.Harris
Internet: Dave.Harris at f14.n15.z1.fidonet.org



More information about the Comp.lang.c mailing list