Re: [C or C++] Is this legal? sizeof *p

From: Jack Klein (jackklein_at_spamcop.net)
Date: 01/04/04


Date: Sun, 04 Jan 2004 01:54:50 GMT

On Sat, 03 Jan 2004 17:17:30 -0500, Jeff Schwab <jeffplus@comcast.net>
wrote in alt.comp.lang.learn.c-c++:

> Jason wrote:
> > {
> > struct a_struct *p;
> >
> > p = malloc( sizeof *p );
> > }
> >
> > That should be legal shouldn't it? Despite the fact that p doesn't point to
> > anything.
> >
> > * For c++ p = static_cast<struct a_struct*> malloc(sizeof *p );
> >
> >
>
>
> Nope. It usually works fine, but don't count on it. The pointer might
> not hold a valid address, and the simple act of dereferencing it might
> cause a bus error. Sorry.

Jeff, you are completely wrong about this one.

In C++, and in C except for one special case in C99, the sizeof
operator is guaranteed not to evaluate its operand, merely use the
operand to identify the type and hence the size.

So this code is perfectly legal in both C and C++.

The special case in C99 that I mentioned above does not apply here.
That is for the a VLA (variable length array) which was added to C in
1999, where the size of an automatic array is defined at run time. In
that case if sizeof is applied to a VLA, it must obviously be
evaluated at run time. But applying sizeof to a VLA is applying
sizeof to an array, not a pointer, and using the name of an array as
an operand of the sizeof operator is one of the few situations when
the array name is NOT silently converted to a pointer.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: Null terminated strings: bad or good?
    ... The sizeof operator yields the size of its operand, ... If the type of the operand is a variable length array type, ... As far as I can tell, the standard doesn't explicitly forbid naming types larger than SIZE_MAX bytes, or even applying sizeof to such types -- it just describes the semantics of sizeof in a way that is logically impossible for such types. ... What this means, in effect, is that it's actually the size of its operand that is implementation-defined; the fact that the value is implementation-defined does not give the implementation the option of having sizeof yield any value other than "the size of it's operand". ...
    (comp.std.c)
  • Re: A couple of things from H&S
    ... expression of array type decays to a pointer to the array's first ... element *unless* it's in one of three contexts: the operand of sizeof, ... is quite obvious, p is the pointer. ...
    (comp.lang.c)
  • Re: Wording glitch: sizeof array vs. sizeof (array)
    ... A parenthesized expression is a primary expression. ... Except when it is the operand of the sizeof operator or the unary ... & operator, or is a string literal used to initialize an array, an ...
    (comp.std.c)
  • Re: sizeof operator in C
    ... All that sizeof needs ... It should only need to evaluate the expression that determines the VLA size. ... some run-time evaluation. ... If the type of the operand is a variable length array type, ...
    (comp.lang.c)
  • Re: Sizeof question
    ... |> | sizeof returns the number of chars that array occupies. ... | On the most common system, a char is 1 byte though. ... operand, which may be an expression or the parenthesized ... operand is a variable length array type, ...
    (alt.comp.lang.learn.c-cpp)