[comp.lang.c] What should "sizeof

greim@sbsvax.UUCP (Michael Greim) (12/06/88)

Hello,

Just recently I tried the following program:
---- cut ----
# include <stdio.h>

struct misty {
	unsigned int a1:1;
	unsigned int a2:15;
};
struct misty mist;
char c1, c2;
int i;
float r;
char s [20];

main ()
{
	printf ("sizeof(mist.a1) [1 bit] = %d\n", sizeof(mist.a1));
	printf ("sizeof(c1<c2) = %d\n", sizeof(c1<c2));
	printf ("sizeof(i+r) = %d\n", sizeof (i+r));
	printf ("sizeof(i+s) = %d\n", sizeof (i+s));
}
---- cut ----
It printed

sizeof(mist.a1) [1 bit] = 4
sizeof(c1<c2) = 4
sizeof(i+r) = 8
sizeof(i+s) = 4

This was on a VAX 11/780 running 4.3BSD.

Now comes my question:
What should "sizeof expression" return? And specially : why does it
return 8 for the third expression?

Thanks for any answers,
	-mg
-- 
email : greim@sbsvax.informatik.uni-saarland.dbp.de
  (some mailers might not like this. Then use greim@sbsvax.uucp)
  or  : ...!uunet!unido!sbsvax!greim
# include <disclaimers/std.h>

seanf@sco.COM (Sean Fagan) (12/08/88)

In article <654@sbsvax.UUCP> greim@sbsvax.UUCP (Michael Greim) writes:
[struct mist { int a1:1, a2:16}; int i; float r; char s[20];

>sizeof(mist.a1) [1 bit] = 4

Because an int, on your machine, is 4 bytes long, and mist.a1 is expanded to
an unsigned int.

>sizeof(c1<c2) = 4

This is an integer expression, 4 bytes.

>sizeof(i+r) = 8

An integer and a floating point form a floating point expression, which, on
your machine, is 8 bytes.

>sizeof(i+s) = 4

This is a pointer expression (equivalent to &s[i]), and a character pointer
on your machine is 4 bytes long.

>What should "sizeof expression" return?

The size, in char's, of the expression.

-- 
Sean Eric Fagan  | "Engineering without management is *ART*"
seanf@sco.UUCP   |     Jeff Johnson (jeffj@sco)
(408) 458-1422   | Any opinions expressed are my own, not my employers'.

evil@arcturus.UUCP (Wade Guthrie) (12/09/88)

Well, I am a relative novice, but how do we learn without trying and
experiencing the flame of disappointment :-)

In article <654@sbsvax.UUCP>, greim@sbsvax.UUCP (Michael Greim) asks
about the output of the sizeof operator in regards to the following
program:

[abridged for the line counter]
> struct misty {unsigned int a1:1; unsigned int a2:15;}mist;
> int i;
> float r;
> char s [20];
> 
> main ()
> {
> 	printf ("sizeof(mist.a1) [1 bit] = %d\n", sizeof(mist.a1));
> 	printf ("sizeof(c1<c2) = %d\n", sizeof(c1<c2));
> 	printf ("sizeof(i+r) = %d\n", sizeof (i+r));
> 	printf ("sizeof(i+s) = %d\n", sizeof (i+s));
> }

well, each of the sizeofs are in the form of sizeof expression, so
let's dicect each expression to figure out what it should print.  First,

sizeof(mist.a1) -- well, my guess (and this is pretty shakey) is that
mist.a1 is really just a single bit of an unsigned int and not an entity
of itself, so its size is that of an unsigned int, which, on the VAX, is
4 bytes.

sizeof(c1<c2) -- this is an expression, the type of which is independent
of the types of c1 and c2.  A "logical" expression in C is just an integer
expression, and an integer requires 4 bytes on a VAX.

sizeof (i+r) -- well, here we have to look at the promotion rules for 
expressions.  On silly compilers (like that on a VAX running VMS),
all floats get promoted to doubles (rather than having the option of
having them not be promoted to allow single precision math like on SUNs,
but I digress).  So, the float r gets promoted to a double, the other
argument of the binary operator '+' gets promoted to a double, and the
result is a double which, on a VAX, requires 8 bytes (am I doing okay
so far?)

and, lastly, sizeof (i+s) --  s the name of an array of 20 chars.  The
name of an array is a pointer to the zeroth element of that array (pointers
require 4 bytes on a VAX).  Adding an int to a pointer (even in this order)
should provide a pointer -- 4 bytes.

does this help?


Wade Guthrie
Rockwell International
Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)

henry@utzoo.uucp (Henry Spencer) (12/11/88)

In article <2976@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes:
>sizeof (i+r) -- well, here we have to look at the promotion rules for 
>expressions.  On silly compilers (like that on a VAX running VMS),
>all floats get promoted to doubles...

Not too silly, actually, since this behavior was required by the old
C standards (K&R and its immediate descendants).
-- 
SunOSish, adj:  requiring      |     Henry Spencer at U of Toronto Zoology
32-bit bug numbers.            | uunet!attcan!utzoo!henry henry@zoo.toronto.edu