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

From: Jeff Schwab (jeffplus_at_comcast.net)
Date: 01/04/04


Date: Sun, 04 Jan 2004 01:38:42 -0500

Jack Klein wrote:
> 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.
>

D'oh! Thank you for correcting me. Of course you are entirely right.



Relevant Pages

  • Ive lied to the compiler and now it wont trust me
    ... pointers to struct o objects (these objects are as stringently aligned as ... actually a pointer to another VLA of pointers to struct o objects (and ... Casting a pointer to an integer is no problem. ... program when I tell it that an element of an array is actually a pointer ...
    (comp.lang.c)
  • Re: How can I get the size of this?
    ... > I am now having a problem at using sizeof() function ... the pointer. ... Operating on something declared as an array returns the size of the ... LPCSTR is already typedef'd as "const char *", ...
    (microsoft.public.vc.language)
  • Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned?
    ... Using SizeOf on variables does not prevent programming mistakes. ... How will you solve the problem for pointer variables? ... For that, I use dynamic arrays, which case I use Length in conjunction with SizeOf to determine the amount of memory occupied by the array data. ...
    (alt.comp.lang.borland-delphi)
  • Re: [C or C++] Is this legal? sizeof *p
    ... In C++, and in C except for one special case in C99, the sizeof ... operand to identify the type and hence the size. ... where the size of an automatic array is defined at run time. ... that case if sizeof is applied to a VLA, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: De-referencing pointer to function-pointer
    ... >> Either your compiler is broken or you are not invoking it as a C ... >> is a constraint violation to apply the sizeof operator to a function ... >> pointer to the element type of the array. ... >> bytes, of the function, not of a pointer to the function. ...
    (comp.lang.c)