tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: definition of NULL correct?



Arnaud Lacombe <lacombar%gmail.com@localhost> wrote:
> Hi,
> 
> On Tue, Aug 11, 2009 at 4:32 PM, Greg Troxel<gdt%ir.bbn.com@localhost> wrote:
>>
>> We define NULL as "(void *)0" rather than "((void *)0)", and that causes
>>
>>  sizeof NULL
>>
>> to fail.
>>
> Wouldn't it more be a gcc issue where it treats the expression as what
> c99 defines as "sizeof (type-name)" (cf section 6.5.3) leaving the "0"
> as a syntax error rather than interpreting the cast and treating the
> sizeof expression as "sizeof unary-expression" ? I'm not sure about
> the precedence of the cast operation over the 'sizeof' operator.

My reading of footnote 71 is that gcc gets the precedence right.

       71)The syntax specifies the precedence of operators  in  the
          evaluation  of  an  expression,  which is the same as the
          order of the major subclauses of this subclause,  highest
          precedence  first.   Thus,  for  example, the expressions
          allowed as the operands of the binary + operator  (6.5.6)
          are  those  expressions  defined  in 6.5.1 through 6.5.6.
          The exceptions are cast expressions (6.5.4)  as  operands
          of  unary  operators  (6.5.3),  [...]

sizeof is unary expression - 6.5.3, cast is 6.5.4.  The exception
mentioned is for the production:

    unary-expr: unary-operator cast-expr

and doesn't apply to sizeof.  So

    sizeof (void *) 0

is correctly parsed as uanry-expr: sizeof ( type-name ) leaving the
"0" as the syntax error.

Neat.  I was always bothered by lack of parens around that cast and
always wondered if there is a context where it gonna bite us...

-uwe



Home | Main Index | Thread Index | Old Index