[m]allocation question

Cameron Simpson,Uhmmm..???? Who knows cameron at usage.csd.oz
Sat Sep 29 15:41:34 AEST 1990


>From article <13946 at smoke.BRL.MIL>, by gwyn at smoke.BRL.MIL (Doug Gwyn):
| In article <UaxqffS00Vtq8akksB at andrew.cmu.edu> ghoti+ at andrew.cmu.edu (Adam Stoller) writes:
|>        t = (struct node *) malloc(sizeof *t);
|>Isn't *t garbage at the time the sizeof is performed - isn't this
|>[almost?] de-referencing a NULL pointer.
| 
| We went through this about a year ago in a different guise.
| The outcome of that discussion was that, since the argument to sizeof
| is NOT evaluated, there is no attempt to access through that pointer,
| and only the type (not the value) of the hypothetical result is
| relevant.  Thus sizeof(*t) is obliged to work even if t contains a
| garbage value.

This interests me. I seem to recall an extended discussion (maybe in
comp.std.c) concerning ways to determine the size of a structure member.
I have been using the macro

    /* size of a field - may break under ANSI, but hasn't yet */
    #define		fsizeof(type,field) sizeof(((type *)NULL)->field)

for some time. I have recollections of remarks to the effect that the
above locution could break under a conformant compiler. Could someone
either correct me ("yes, Cameron, fsizeof() is ok") or re-iterate the
explaination of why this macro can break, while sizeof(*t) is fine.
	- Cameron Simpson
	  cameron at spectrum.cs.unsw.oz.au



More information about the Comp.std.c mailing list