bison-patches
[Top][All Lists]
Advanced

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

Re: RFC: maint: c++: use nullptr for C++11


From: Paul Eggert
Subject: Re: RFC: maint: c++: use nullptr for C++11
Date: Fri, 30 Mar 2012 11:15:37 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.1) Gecko/20120209 Thunderbird/10.0.1

On 03/30/2012 07:38 AM, Akim Demaille wrote:

> I am explicitly expecting NULL to be defined for C

That's not a portable assumption, since NULL is defined in
include files like <stddef.h>, and the C parsers try to avoid
including <stddef.h> etc. in order to keep the name-space as
clean as possible.  At least, yacc.c does that; I didn't check
the others.

Instead, I suggest using the same definition for YY_NULL in
C as in C++, namely this one:

   # ifndef YY_NULL
   #  if 201103L <= __cplusplus
   #   define YY_NULL nullptr
   #  else
   #   define YY_NULL 0
   #  endif
   # endif

> I've been told many times that NULL needs not be 0, but I
> have just never seen that, and given the idiom "if (cp)",
> I really wonder if it does exist).

Yes it does, but it's more complicated than that...
Basically, any C implementation can put this into
<stddef.h>, and it will conform to the standard:

   #define NULL 0

A C implementation is also allowed to put this into <stddef.h>:

   #define NULL ((void *) 0)

and this will also conform to C89 or later; but this latter
approach is less portable to ancient compilers so for Bison
plain 0 is probably better.

This works because if P is a pointer variable, a C compiler
is obliged to treat 'P == 0' as a test for a null pointer,
even if the internal representation of a pointer has some
nonzero bits, and likewise it is required to treat 'P = 0'
as an assignment of a null pointer regardless of how null
pointers are represented.  So '#define NULL 0' is always
safe.

Details of this are in section 6.3.2.3 in the C11 standard.


By the way, thanks for your efforts to improve Bison!
It needs it, and your work is appreciated.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]