User forums > Help

NULL undefined

<< < (3/8) > >>

killerbot:

--- Quote ---The macro NULL is an implementation-defined C++ null pointer
--- End quote ---

Implementation-defined !!!! --> NOT standard.

It shows up in so many headers, why ... because it is not standard. People just define it in headers because they are used to using it. I have been attending many talks of Mr Stroustrup [EDIT : fixed typo in Bjarne's name] where he is stressing it is not standard [maybe just for the fact that it is not portable], in his code samples he writes 0.
In fact it is a real violation of strong types.

char* p = NULL;  char* != void* [in case it is defined as just 0 it would be ok].

If it is so standard, why are there so many compilers who do NOT define it, some do it in stdio.h others in stdlib.h, correction : cstdio and cstdlib. Trust me, I have been using a lot of compilers and I have seen code break because of the lovely NULL not being defined.

Darn right let's include a huge header file to get the NULL define, and on the other hand we pimpl and other stuff to get less include dependencies, but let's break all that for that nice lovely NULL.

If I recall correctly, they are thinking about a *template* for something like nullptr, but I am not sure.
For sure it will be a keyword --> so part of the language :

--- Quote ---nullptr - C++0x keyword for the null pointer. It is not an integer. It can be assigned only to pointers.
--- End quote ---

a define is not part of the language, it is the same story that some compilers have a define for PI and others don't.

And I have heard/read similar things from Sutter, Meyers about NULL not being standard. I am wrong if they didn't tell/write the correct stuff [maybe just for the fact that it is not portable]. But I will never ever use NULL for sure as those gurus suggested. I want my code to be clean not including headers (which in turn include other headers) for some freaky define where I kind write something as simple as :
char* p = 0;

By the way, NULL and 0 have another problem, but when you write 0 it becomes more apparent :

f(int);
f(char*);

f(0); ---> which one am I calling ......... ==> we call f(int) !!!
if we want to call f(char*) we should :
f(static_cast<char*>NULL);
f(static_cast<char*>0);

And this is where that keyword nullptr will help us out, because it is not an int and can only be assigned to pointers.

Which brings us to :
f(int*);
f(char*);

--> f(nullptr) ---> ?????

On the other hand, we better follow Meyers advice : Avoid overloading on a pointer and a numerical type. See Item 25 in his book "Effective C++". Where he discussed 'what is 0 (pointer) '


Another note : NULL is a preprocessor symbol as said --> so not the language.
See C++ Gotchas from Dewhurst : Gotcha 9 : Using bad language :

--- Quote ---The trouble was that NULL would be defined in various ways on different platforms :
#define NULL ((char*)0)
#define NULL ((void*)0)
#define NULL 0
These various definitions wreaked havoc with portability of C++ programs [My note : As said already above]
In fact there is no way to represent a null pointer directly in C++, but we're guaranteed that the numeric literal 0 in convertible to a null pointer. That is what C++ programmers traditionally used to ensure the portability and correctness of their code. Now the standard indicates that definitions like (void*)0 are not allowed.

--- End quote ---

Wrapping up : just avoid using NULL, as said it is not professional and makes your code break on different platforms.
Which header should bring that NULL (null.h ?? woops doesn't exist) ?
Is it now in the standard or not, I still believe it is not, standard means it is something that is guaranteed, not implementation specific. They keyword 'while' guarantees me something, but the preprocessor symbol NULL guarantees me nothing.

Really, avoid NULL, use 0. There are only benefits.



--- Quote ---d also the C++ language; and its use is professional: it makes software more readable, less ambiguous and more maintainable.
--- End quote ---
Yes really, it breaks code. i wonder why Dewhurst called his topic 'Bad language'.
Just tell the original poster which header to include, and tell him that the include of that header will bring compilation time down [for the little example it is no issue, but on bigger software] just because NULL is professional. You are also very welcome to port his code then to embedded devices because it for sure will break [have seen it several times] because that iostream header doesn't even exist, or it doesn't bring the NULL.
Meyers, Stroustrup, Sutter, Alexandrescu, Dewhurst ... , they all use 0 and not NULL. Those guys live, breath C++, but it seems they are unprofessional because they don't use NULL.
I respect your opinion but I totally disagree. I prefer fast compiling , portable , sound C++ code. Your NULL use will make everything explode, sorry.


--- Quote ---If we look at the code we want to see immediately if something is a "null pointer constant" or the zero integer.
--- End quote ---
This brings nothing :
char* p = 0;  // or NULL  (what's the difference to the eye ???) it is all on the same line.

if(p== NULL) and if (p!= NULL)

just write
if(!p) and if(p) // we know p is a pointer so it is very obvious what the test does, no need to type those extra things.

With the null pointer keyword coming up, I agree then that should be the style to use, but then it comes from within the language, no need to include something [just like 'while' comes without any include].



--- Quote ---#define nullptr 0
--- End quote ---
Suggestion : put it in your personal namespace so you don't get any conflicts. Once the C++0x is there you can replace all MyNameSpace::nullptr by nullptr. So make a constant out of it instead of a define. And that constant lives in its namespace, your define is now everywhere and can fight with other doing a similar thing.


NULL being defined in several headers ---> one definition in one place seems no longer a sound advice ??? Once again another sound programming idiom broken thanks to those nice 4 characters, N U L L .

JGM:
The man really knows what he's saying! :)

wwolf:

--- Quote ---Implementation-defined !!!! --> NOT standard.
--- End quote ---

I happen to be an invited expert who works in the C++ standardization committee. I can assure you, that the fact that NULLs replacement tokens are implementation defined does not make it non-standard.  I am afraid to say, but I have to: your ranting here only shows your complete ignorance about the language and its rules.  And it does not only hurt your reputation, but also hurts all those people who read it. It hurts those who know what damage you cause because they see the bogon flux rising, and it hurts even more those, who may believe a word of what you say.
--- Quote ---
--- End quote ---


--- Quote ---It shows up in so many headers, why ... because it is not standard.
--- End quote ---

Wrong again.  It shows up, because it is standard and the standard requires it to be there.


--- Quote ---People just define it in headers because they are used to using it.
--- End quote ---

And wrong yet again.  People do not define it.  Standard C and C++ library implementers define it.  And not because they are used to it, but because the standard requires them to do so.


--- Quote ---I have been attending many talks of Mr Stroustroup where he is stressing it is not standard
--- End quote ---

And I do not know what talks you have heard and completely misunderstood, but NULL is standard and it is portable.  But I will ask Bjarne.  We talk frequently and I am going to meet him next week in Kona.


--- Quote ---[maybe just for the fact that it is not portable], in his code samples he writes 0.
In fact it is a real violation of strong types.
--- End quote ---

???  Using an INTEGER instead of a pointer is not???  Man, you are going out of your way to find any excuse for your incompetence, but all you do is show how this is a matter of pride for you instead of a matter of knowledge or understanding.

What Bjarne may have told you is not to define NULL yourself.  And yes, ages ago (10-15 years ago) when C libraries have not yet been updated to follow the C++ standard you may have ended up with a bad NULL definition from a rogue header.  10-15 years ago!


--- Quote ---If it is so standard, why are there so many compilers who do NOT define it, some do it in stdio.h others in stdlib.h, correction : cstdio and cstdlib. Trust me, I have been using a lot of compilers and I have seen code break because of the lovely NULL not being defined.
--- End quote ---

You have used non-standard compilers.  I assume it was ancient gcc and MSVC6.  Is it really so hard to understand the difference between a standard and its (possibly faulty) implementations?


--- Quote ---Darn right let's include a huge header file to get the NULL define, and on the other hand we pimpl and other stuff to get less include dependencies, but let's break all that for that nice lovely NULL.
--- End quote ---

Meaningless ranting ignored...


--- Quote ---If I recall correctly, they are thinking about a *template* for something like nullptr, but I am not sure.
For sure it will be a keyword --> so part of the language :
--- End quote ---

Just go to the committee site and read the paper. It has been voted into the working draft.  I do not remember if they have decided about a core language extension (keyword) or a pure library implementation.  Doesn't really matter, since it has nothing to do with the fact that NULL is standard.


--- Quote ---a define is not part of the language, it is the same story that some compilers have a define for PI and others don't.
--- End quote ---

Wrong again. The standard C and C++ library are part of the C++ language. Please stop disseminating false information! If you do not understand the difference between the core language and the library - please learn it!  But do not talk nonsense!


--- Quote ---And I have heard/read similar things from Sutter, Meyers about NULL not being standard.
--- End quote ---

That is a plain lie.  Herb and Scott happen to be my good friends, and I would know if they had fundamental misunderstanding like that. They don't. Herb knows the whole standard inside out, and would not say such a silly thing. Same for Scott.  BTW if you happen to own Scotts Effective C++ 3rd edition look at the acknowledgments chapter.  You will find my name there in the same sentence with Herb Sutter's.  I am not saying this to show off, but to show you that I know what I am talking about. In case you did not believe me - which seems to be happening.


--- Quote ---Wrapping up : just avoid using NULL, as said it is not professional and makes your code break on different platforms.
--- End quote ---

Since this advice of your is based on your completely misunderstanding of the topic I must admit that the above warning is what is unprofessional. Especially stating it as a fact instead of an opinion (what it really is, and a faulty one).

My suggestion to you is that instead of ranting buy books and read them. Learn what C++ is.  And then, and only then, if you can come up with specific examples of non-portability, we will listen. Until then I have to warn all the readers that you have no clue what you are talking about, and that your advice is not only unsound but outright dangerous. Your lack of professionalism is clearly shown in many aspects, but one of the more important one is that you fail to provide any specifics of any broken platforms.

BTW I haven't programmed on too many platforms. Only on Linux, Solaris, BSD, Windows and 16 bit Windows. So I cannot talk about how broken embedded environments are, but one thing I cal tell you: in the last 10 years I have not seen any modern compiler platform where NULL would have been broken on a way that programs did not compile.  No wonder: it is standard for approx. 10 years now.

wwolf:
BTW Bjane's name is spelled Stroustrup.  If you (mis)quote him, at least get his name right.  :lol:

wwolf:
 :lol:Bjarne's :lol: :? :lol:

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version