[comp.lang.c] Help needed with: sizeof

dave@aspect.UUCP (Dave Corcoran) (06/20/91)

printf("Hello World\n");

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
-- 
David Corcoran		      -@@
uunet!aspect!dave	        ~
Having the right to do something is not the same as being right in doing it.
					--  C.K. Chesterson

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (06/20/91)

In article <10569@aspect.UUCP>, dave@aspect.UUCP (Dave Corcoran) writes:
> My Sun cc prints 4 for both structs, prints 3 for the array
> struct a {char x;char y;char z};
> struct b {char x[3]};
> char c[3];

Syntax error:  that should be
	struct a {char x; char y; char z;};
					^
and the same for struct b.  Omitting the last ; before the } of a struct
was a Berkeley extension.  Many compilers do not support it, and it is
not standard.

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

Four bytes *IS* the actual size of the structs in that implementation of C.
Remember, "sizeof (T)" means "if you had an array of N elements each of
type (T), the number of bytes in the array would be N times <what>?".
If you have an array of (struct a)s, they will be 4 bytes apart, not 3.

There isn't anything you can portably do that would profit from trying
to outsmart the compiler in this way, and very little that is _safe_
even for a program that is never going to see another compiler or machine.

(You could try taking offsetof the last field + sizeof the last field.
But of course the last field itself may contain padding.)
-- 
I agree with Jim Giles about many of the deficiencies of present UNIX.

Dave.Harris@f14.n15.z1.fidonet.org (Dave Harris) (06/22/91)

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@f14.n15.z1.fidonet.org

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/22/91)

In article <10569@aspect.UUCP>, dave@aspect.UUCP (Dave Corcoran) writes:
> My Sun cc prints 4 for both structs, prints 3 for the array

> struct a {char x;char y;char z};
> struct b {char x[3]};
> char c[3];
[code to print out sizeof() the above things omitted -dM]

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

It *is* returning the actual size of the structs.  The size of a struct
is defined to include any padding the compiler sees fit to include.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu