Help needed with: sizeof (struct a)
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Thu Jun 20 17:11:47 AEST 1991
In article <10569 at aspect.UUCP>, dave at 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.
More information about the Comp.lang.c
mailing list