sizeof a struc field

kenny at m.cs.uiuc.edu kenny at m.cs.uiuc.edu
Thu Oct 26 03:25:00 AEST 1989


Hey, wait a minute here.

Is it just the constant NULL that can't be `dereferenced' in a sizeof
expression, or is it any possibly invalid pointer?  The discussion
seems to indicate that the common idiomatic macro definition

#define NEW(p) ((p) = malloc_and_check ((size_t) (sizeof *(p))))

(assuming that malloc-and-check never returns NULL, but throws a
signal, longjmps, or something), is illegal under the proposed
standard, because it dereferences the uninitialized pointer p.  I find
it hard to believe that this behavior was the intent of the Committee.

If this interpretation is correct, I can still live with it by making
a NEW macro for every type (typeof wasn't accepted, so I can't
determine the size of the object without knowing the name of the type,
unless I can write sizeof *p), or by passing the type name as a macro
parameter, but it's pretty bogus.  I hope at least that the Standard
doesn't require that I go out of my way to make this NOT work.  I
can't imagine any way that a sane compiler writer could have this
fail, since you never even need to load the pointer p -- all the
discussion of possible faults upon loading a pointer into a register
doesn't apply, since this expression never examines the value of the
pointer at all.

I would submit that `dereferencing' a pointer should mean reading from
or writing into the location to which it points.  Just putting a * or
-> on it doesn't do this in C.

Kevin

| /         o            Kevin Kenny   KB9DLU                    (217) 333-5821
|<  /) |  | | |/\        Department of Computer Science           o  ,    o  ,
| \ X_  \/  | | |        University of Illinois                 40 07 N 88 13 W
kenny at cs.uiuc.edu        1304 W. Springfield Ave.       
uunet!uiucdcs!kenny      Urbana, IL   61801                  AD ASTRA PER ARDUA
k-kenny at uiuc.edu
kenny%cs at uiucvmd.bitnet



More information about the Comp.lang.c mailing list