Re: [PATCH] Replace NULL symbol with 0



Dnia 01-08-2011 o 13:43:42 Dodji Seketeli <dodji seketeli org> napisał(a):

Tomasz Kupczyk <kupczyk tomasz gmail com> a écrit:

Hello all,

Hello Tomasz.  :-)

I'm new on the Nemiver mailing list,

You are welcome!

I haven't written any code in Nemiver yet, so maybe I shouldn't
comment on the code here,

I disagree.  You are free to comment here, no matter who you are.  I for
one am always interested in technical matters related to Nemiver.  :-)

Good to hear that :)


wondering what is the idea behind changing all NULL's to 0's?

As far as I know NULL equal to 0 is platform dependent, but on most
platforms probably it's indeed 0.

The C++ specification says, in [conv.ptr]/1:

    A null pointer constant is an integral constant expression (5.19)
    prvalue of integer type that evaluates to zero

So, the constant 0 is a perfectly valid platform independent null
pointer constant in C++, unlike in C.

Ok, I've also checked, this explains a lot.


When you use g++, NULL is a macro defined to __null, which is a special,
g++ specific symbol that does the right thing, i.e, it evaluates to
zero.

Furthermore, NULL being a macro, if you want to see its integral
representation in a debugger, you have to have to e.g, have some arcane
compiler flag enabled, as just -g is not enough.  If you use zero, in
the debugger, you know easily that it is zero.

Different thing is that IMO using NULL makes sometimes code easier to
read,

I understand that conveying the idea that "this is a null pointer
constant" is more maintainable (as in, "it tells a better story to the
reader's mind") than just 0, when using a context that expects a
pointer.  But NULL is not necessarily correct.  That why c++0x
introduces the null pointer constant nullptr, of type nullptr_t.

When is NULL not necessarily correct? I suppose it's better to read
some rationale why nullptr is introduced :)


For now, my take was that it's less confusing to separate NULL which
originates from C and which is platform dependant, as you pointed out,
from 0.  And use 0 instead.  When you are used it, it zero can also tell
a nice story to the reader's mind, I believe.  :-)

I am happy to let pieces of code that are compiled with a C compiler use
NULL.

and for example if in the future compilers will start to enforce
semantically null pointers being really NULL then all 0's will need to
be turned into NULL's once again.

I don't understand this.  nullptr of c++0x comes with an implicit
conversion that can convert it to 0.  So using 0 will still work in
c++0x.


I was thinking about type checking, for example when you write void* ptr = 0, 0 is of integral type, and ptr is of void* type, but the conversion succeeds, but when you write void* ptr = 10 you'll get an error (gcc gives a warning only):

error: invalid conversion from ‘int’ to ‘void*’

I thought that in future it could happen that assigning 0 to a pointer type
will be treated the same way as assinging 10, because there will be no special
case for 0.

But generally yeah, it's a digression, thank you for your answers :)

Best regards,
Tomasz Kupczyk


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