Author Topic: NULL undefined  (Read 24468 times)

JaR

  • Guest
NULL undefined
« on: September 15, 2007, 11:11:53 am »
Hello,
I just realized, that, in the following code, the compiler returns an error, because of NULL being undefined. Is this a bug within the gcc, should CB define the macro itself or is it just not defined in standard C++?

Code: [Select]
int main ()
{
  int n = NULL;
  return 0;
}

if i #include <iostream> first, there is no problem.

JaR

Offline XayC

  • Multiple posting newcomer
  • *
  • Posts: 94
Re: NULL undefined
« Reply #1 on: September 15, 2007, 11:15:23 am »
NULL is not part of the C or C++ *, but it's usually defined in some headers.
So yes you have to define it, and it's not a Gcc bug.

Code: [Select]
#define NULL 0                   // C++ definition.
#define NULL ((void*)0)          // C definition.

Edit: * With "not part of the C or C++" I mean not a language keyword. It does not mean you have to define it, because it's defined in the standard headers.

Regards, XayC
« Last Edit: September 17, 2007, 06:45:23 pm by XayC »

JaR

  • Guest
Re: NULL undefined
« Reply #2 on: September 15, 2007, 11:22:55 am »
Thanks for the quick reply.
Edit: I think the definition is not supposed to have a semicolon.

JaR
« Last Edit: September 15, 2007, 12:18:40 pm by JaR »

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: NULL undefined
« Reply #3 on: September 15, 2007, 08:13:04 pm »
Edit: I think the definition is not supposed to have a semicolon.
Correct.  That will make the semicolons themselves be part of the text replace the preprocessor performs.  That aside id also use a global const int and not a define.  Then again I'm also anal retentive about type issues.  =-)

Offline XayC

  • Multiple posting newcomer
  • *
  • Posts: 94
Re: NULL undefined
« Reply #4 on: September 15, 2007, 09:41:46 pm »
I think the definition is not supposed to have a semicolon.
Yes you are right: edited. Thanks for pointing it out.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: NULL undefined
« Reply #5 on: September 15, 2007, 11:11:34 pm »
just never use NULL? it is not standard C/C++.
Why would you use it, it is just not professional.

If we want to be stupid and use NULL, well then we can as well use ZERO, now let's hope our compiler 'defines' it for us.
Bweurk, #define, yet another non professional thing ...

Offline oZZ

  • Multiple posting newcomer
  • *
  • Posts: 30
Re: NULL undefined
« Reply #6 on: September 16, 2007, 03:17:30 am »
I #define nullptr 0 for the time being.  It's being added to the C++ standard in the next revision as something different than 0 to give a little more safety to null pointers, so it's probably a better way to do it for now.  Just remember to remove the #define when C++0x hits.

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #7 on: September 16, 2007, 03:37:42 am »
Quote
just never use NULL? it is not standard C/C++.
Why would you use it, it is just not professional.

If we want to be stupid and use NULL, well then we can as well use ZERO, now let's hope our compiler 'defines' it for us.
Bweurk, #define, yet another non professional thing ...

Please refrain from presenting such (mistaken) opinions as facts.  NULL is standard.  If you do not believe me, buy the standard.  It is 18USD in PDF format and you can also buy it from Wiley and sons in book format.  Until you have and understood the standard please refrain yourself from statements based on bad or misunderstood knowledge.  You are not only putting a bad light onto yourself (in the eyes of those who really know C++ and have experiences with it) but you also hurt those beginners who believe you.

NULL in C and C++ is standard.  It is actually defined in more than one standard header.  It is so much standard, that C++0x (the new C++ standard) is going to introduce a special type to represent NULL pointers (of any kind).

Why is NULL important?  Simply because it is a name.  Of course, we can write 0 in C++. Yes.  And we could also work in assembler, and drop C++ completely.  But we want to write readable and maintainable code.  If we look at the code we want to see immediately if something is a "null pointer constant" or the zero integer. There is no difference between the two for the compiler, but there is a logical, conceptual difference between them on the design level. (That is the reason why nullptr is being introduced into standard C++ - to make the null pointer constant different for the compiler as well, not only for the human reader.)

So, I have to strongly disagree with your assessment on NULL and kindly ask you to think twice before you post such nonsense. You actually hurt people with these kind of statements. If they believe you, they will flank exams, job interviews, develop bad programming practices - and perhaps learn from you making technical statements based on gut feelings instead of knowledge. It might work in politics, but it is not a winning strategy in an engineering discipline. I know, I was there myself. :)

So I am afraid it is not NULL that is not professional, but your post. :(  NULL is standard - part of the ISO standard C and also the C++ language; and its use is professional: it makes software more readable, less ambiguous and more maintainable.

Offline TDragon

  • Lives here!
  • ****
  • Posts: 936
    • Twilight Dragon Media
Re: NULL undefined
« Reply #8 on: September 16, 2007, 04:59:27 am »
NULL is standard.  If you do not believe me, buy the standard.
In case anyone is wondering:
Quote from: ISO/IEC 14882:1998 18.1 - Types
-4- The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (conv.ptr).*
[Footnote: Possible definitions include 0 and 0L, but not (void*)0. --- end foonote]
http://tdm-gcc.tdragon.net/ - TDM-GCC compiler suite for Windows (GCC 5.1.0 2015-06-28, 32/64-bit, no extra DLLs)

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #9 on: September 16, 2007, 07:33:57 am »
Thanks. I did not want to quote it, because I was not sure that it would convince someone who wrote something like the original post I have replied to.  And in case anyone is wondering why do I spend time to write a long post about this... Well, let's just say that I have lost 5 years of my life on fixing crap code just because Mr. Bullschildt wrote a book based on the same kind of "knowledge" demonstrated by the OP, and my company bought that book. Before I started to work for them. It took at least 5 years of hard work to make people unlearn the enormous amount of crap and "applied misunderstanding" they have gathered from the book and from similar "opinionated guesswork" kind of "learning" as was demonstrated by the OP.

In other words: I have seen and suffered the damage that amateur, guesswork based approaches can lead to. I was lucky that my company is huge. Had I have met this level of incompetence in a smaller shop, it would have taken the place down. And when I say incompetence I do not mean people who are ignorant and/or do not want to learn. I mean that people have been taught outright crap by books and by "well meaning" individuals who taught their misunderstandings and lack of knowledge (with lack of experience) as fact.

Software engineering (although we successfully demonstrate many sings of the contrary) is neither a religion, nor fashion, nor politics. Believing and liking/disliking has no place in making technical decisions. Only facts do, and thinking. And that is what I have attempted to promote. Even if I sounded like a j*rk or a smart S. ;)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: NULL undefined
« Reply #10 on: September 16, 2007, 10:01:41 am »
Quote
The macro NULL is an implementation-defined C++ null pointer

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.

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.

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.
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.
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
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 .
« Last Edit: September 18, 2007, 12:28:40 pm by killerbot »

Offline JGM

  • Lives here!
  • ****
  • Posts: 518
  • Got to practice :)
Re: NULL undefined
« Reply #11 on: September 16, 2007, 07:18:12 pm »
The man really knows what he's saying! :)

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #12 on: September 17, 2007, 02:49:46 pm »
Quote
Implementation-defined !!!! --> NOT standard.

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

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

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.

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

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.

???  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.

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.

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 :

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.

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.

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.

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.

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #13 on: September 17, 2007, 02:51:48 pm »
BTW Bjane's name is spelled Stroustrup.  If you (mis)quote him, at least get his name right.  :lol:

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #14 on: September 17, 2007, 02:57:06 pm »
 :lol:Bjarne's :lol: :? :lol: