Author Topic: NULL undefined  (Read 24466 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:

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: NULL undefined
« Reply #15 on: September 17, 2007, 06:00:45 pm »
NULLs existance is mentioned in the standard but its use is left open to interpretation. Thus NULL itself is NOT standard.  There is no enforced definition thus no stable use.  So if you are trying to be a no-it-all professional thats fine, as long as you are informative.  But you're being rude and pompous while being wrong.  At least killer's (obvious to everyone but you) opinions ARE being helpful.

Offline TDragon

  • Lives here!
  • ****
  • Posts: 936
    • Twilight Dragon Media
Re: NULL undefined
« Reply #16 on: September 17, 2007, 06:38:22 pm »
Perhaps everyone could just take some time to read and understand the ISO C++ standard. In section 18 (Language support library), subsection 1 (Types), it mandates the existence of a macro, defined in <cstddef>, whose identifier is "NULL" and whose replacement list is implementation-defined. NULL is "standard", folks. The argument of whether or not it's appropriate to be used I'll leave to others, but make sure you have your facts straight.

By the way, this topic has reached the point where it's probably more appropriate for comp.lang.c++, not the Code::Blocks forums. Just a thought.
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 killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: NULL undefined
« Reply #17 on: September 18, 2007, 12:17:11 pm »
Quote
In section 18 (Language support library), subsection 1 (Types), it mandates the existence of a macro, defined in <cstddef>, whose identifier is "NULL" and whose replacement list is implementation-defined. NULL is "standard",

Ok, that's black on white, and it even says in what header it should be. You convinced me. So people me saying it is not standard is wrong.
Though my arguments on why not to use it still stand. As mentioned, when you read those books, you see those gurus don't use NULL.

Core language and extension libraries make up the standard : that is absolutely correct. I never said the opposite, how else could stl be standard.

[time to fix the spelling mistakes in the names ;-) , Firefox already told me the name was wrong  :P]

And nullptr will be a keyword (check Bjarne's site).

NOTE : wwolf's words are not considered rude by me. He showed me wrong about NULL, it is standard. I made a mistake by believing hints and other suggestions/advices about NULL. As TDragon quoted from the standard, it is obvious.
But as said above, I have put up a some arguments not to use it. It will make things easier.
So let's drop the unprofessional (over)quote of mine. But by not using it, you are professional, since it does not bring you that much benefits, it does bring drawbacks, and as said others also see NULL usage as bad language and the gurus also don't seem to use it. I personally like to follow their coding style and suggestions.

Time to move to 'comp.lang.c++' ;-)

According to TDragon's quote of the standard it should be defined in cstddef, though it seems several compilers also like to define it in other headers through ifndef constructs, let's define it anyway.
Example : TI embedded compiler, the define can be found in : stddef.h/stdio.h/stdlib.h/string.h/time.h.
That means if you did not correctly include cstddef your code might compile because of those other header you might encounter in your include list (perhaps a few levels deeper). But if another compiler did not define it in the header you got lucky wih, your code breaks.
So either always do it through cstddef, or better use '0'.
« Last Edit: September 18, 2007, 12:49:42 pm by killerbot »

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #18 on: September 18, 2007, 01:28:06 pm »
Killers opinions are bullfeces, trying to stay PC.  IOWs is it all crap. NULL is standard.  If you want, I will get for you a signed paper from Bjarne and Herb. Could you guys please stop this madness??? Youth and its enthusiasm is admirable, but there are times when you just need to learn and live with the facts.  NULL is not perfect, but it is standard.  That is a fact.  The rest is crap.

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #19 on: September 18, 2007, 01:35:49 pm »
BTW thanks for not taking my words as an offense. I am not trying to be an S whole... but I have a birth defect. Whatever I say tends to come out is if it used the wrong end... ;)

Anyways, I myself tended to drop NULL in favor of 0 but in live code I have realized that seeing NULL there helps a lot when you are reviewing or integrating the code. A simple text search for NULL will (for example) find all those crappy classes that are doing memory management while they are there to represent application domain concepts. IOWs NULL is a notational convenience that helps the human reader and it helps the text search - when needed.  And I think we all agree that the new nullptr is very welcome. :)

Thanks for clearing it up that the core language solution was the one finally chosen. I have not remembered that decision.

BTW if you do not want to remember (I mean y'all ;) the long URL for the C++ standards site (open-standards.org/something/something) then feel free to use my domain http://stdcpp.org.  I have made it to HTTP redirect to the real site. Easier to remember for me...

WW aka Attila
Ps: Unfortunately I am not really able to (easily) use USENET nowadays. Unfortunately my ISP does not enable access if I am not going from their network. :( So I am not really present on the newsgroups nowadays.

Offline JGM

  • Lives here!
  • ****
  • Posts: 518
  • Got to practice :)
Re: NULL undefined
« Reply #20 on: September 18, 2007, 03:45:25 pm »
I have learned a lot from this discussion, jajaja  :D

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: NULL undefined
« Reply #21 on: September 18, 2007, 03:50:16 pm »
just checked a few more compilers :  Digital Mars, MSVC8[aka 2005], OpenWatcom. Most of them define NULL in a *lot* of headers. In this is really causing a lot of code to break (in MingW I have the impression it is only in stddef, Borland does it in _null.h) because very little people are including the correct header.  So we could blame compilers, users, but not the standard.

And the arguments are not crap, sorry :
 - 1 definition in 1 place [but that's what the standard says according to the above quote : cstddef], but s you can see your compiler doesn't always do it
 - minimize compiling dependencies [using 0 --> no include needed]
 - implementation dependent , who will tell how a compiler vendor decided to implement it [an old implementation is according to the standard [as in Dewhurst's book] no longer accepted]
 - the lesser of the PreProcessor 'beast' can be used, the better [there are always valid use cases for the PP, but way little then people tend to think]


PS : gonna try to find out where I 'mislearned' the fact NULL is not standard. I got it out of a book or in a C++ presentation (by well respected people), because I remember I was surprised at the time and later on changed my code and style, since I used NULL before that day. But won't ever use it again ;-)
« Last Edit: September 18, 2007, 08:55:24 pm by killerbot »

Offline orefa

  • Multiple posting newcomer
  • *
  • Posts: 102
Re: NULL undefined
« Reply #22 on: September 18, 2007, 06:19:42 pm »
[...] bullfeces [...] crap [...] madness [...] crap.

Whatever I say tends to come out is if it used the wrong end...

Gosh, I wonder why. I also wonder why you continued this even after Killerbot corrected himself, showing a good deal more maturity than yourself.

Anyway, I also did learn something from this thread, both about C++ and about the character of the participants.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: NULL undefined
« Reply #23 on: September 18, 2007, 06:24:59 pm »
let's remember we gained knowledge about C++.

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #24 on: September 18, 2007, 08:26:47 pm »
Let's assume that killerbot is able to get offended without your help.  :P  If he wants to. Apparently, that isn't the case.

BTW the reason I have written those words down (thank you for misquoting them by removing the parts that has softened their meanings, you have really made a great disservice to yourself to use such politician's moves) is simple. We have been going around and around in a topic, where facts where misrepresented. Facts, such as that NULL is standard, and (lately) that Digital Mars is far from being standard. MSVC later editions are somewhat better than 6.0 was, but MS still owes us a lot of fixes - where their compiler does not follow the standard. I do not think NULL falls into this category tough...

Anyways, when the sky is blue and the grass is green, and someone insists it is not so, because God told him so... At some point of time the discussion has to be cut short. Mainly to limit the damage. In this case, I had to tell that the "non-standard" part is nonsense, and the non-portable part is a weak argument nowadays. And I have also presented practical reasons why NULL can be a good choice.

BTW it seems that killerbot was able to understand that I have been judging the technical content of what he has said, and not him. And yes, I have told my opinion about his behavior (not reading the standard, ranting etc.) - mainly because I felt this was part of why we could not communicate... but only exchange posts.

Anyways, my goal was not to upset anyone, but to clarify the situation and to (kindly;) ) ask that people please tell when they are sounding an opinion (as opposed to citing facts) and also to tell the reason(s) why they hold that opinion. You are free to point out if I miss that myself. I do it, I know. :)

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #25 on: September 18, 2007, 08:36:13 pm »
killerbot,

I might remember wrong, but I think the standard itself probably already requires NULL to be defined in more than one header. And if it is defined in non-standard headers (such as header that are not listed in the C or C++ standard) they are bad implementations. People should stop defining NULL and include the appropriate standard header instead...

BTW it is no problem (in practice) if they define NULL in a standard way, and if they only define it if it has not been defined already.  Which is (I think) fairly simple:

#ifndef NULL
#ifdef __cpluplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

But as the standard clearly moves the responsibility of defining NULL and its replacement tokens into the "rights" of the implementor (in our case the C and C++ standard library implementor) the above code must be replaced by the inclusion of the appropriate standard header. Yes, it works in most cases but it is just unnecessary code duplication - and it is error prone. For example if NULL gets defined to be nullptr, the above code will not work anymore properly.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: NULL undefined
« Reply #26 on: September 19, 2007, 02:39:55 pm »
My personal issue with NULL is that it is a macro, and I just don't like macros. That's it.
Yes, there are good uses for macros (anyone who ever needed offsetof(), will agree), but most macro uses are... what was that word above, bull feces? :)
A macro replaces some text of yours with some other text. You don't know if it happens, you don't see what happens, and the preprocessor doesn't care about the language's grammar, namespaces, or much of anything else. This can be a desaster, but it can also be a good thing in a few rare cases. It really depends.

If I remember correctly, there was a Stroustrup quote 10+ years ago that said something similar in respect to NULL. I think it was like "use what you want, it's the same thing, but I'm using 0 because I don't like macros".

That's the correct way of looking at it, in my opinion. What does it matter if NULL is standard or not? It is a descriptive token. If you want something to be descriptive, make it NULL. If you don't want that, make it 0. Whatever you do, it's ok.
WM_CLOSE and WM_LBUTTONDOWN are not standard, but they prevent you from wondering "What the hell do 16 and 513 stand for, what is going on here?". Or think about expressions like INT_MAX-1
To me, if(ptr == 0) is pretty darn obvious, and I prefer this form in almost all cases. To other people, it is maybe not so obvious. In either case, I would use const in preference to #defines. While #defines have their good uses, in many cases, a const performs the same, but safer.

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.
What is wrong with static const void *nullptr = 0;? It obviously won't do for foo_t* bar = nullptr; without a cast, but if(foo == nullptr) should do just fine, and would bark if it encounters pointer/integral mismatches.

If you want to be able to assign nullptr too, it obviously gets more complicated... but you could write something like: template<typename T> void NullPtr(T& ptr) { ptr = static_cast<T>(const_cast<void*>(nullptr)); } and then NullPtr(some_pointer); to zero that pointer. I haven't tested this, but I guess it should just work.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #27 on: September 19, 2007, 03:38:55 pm »
Yes, it is a macro. However the general bashing of macros does not apply to it. Why? Because it represents something that is GLOBAL, that must not obey namespaces or anything like that.

I know that Bjarne does not like the preprocessor, however since he has failed to provide (originally) a better solution (that can do the same things) we need to use it, since we have nothing else. This is my general comment. And if you look at (for example) the Boost Preprocessor Metaprogramming Library you will see, that the preprocessor has its place.

As for 0 vs. NULL: the fact that NULL is a macro is irrelevant here. Even if Bjarne says otherwise.  What you decide between is using a named entity versus a literal integer.  To me, after approx. 7 years large scale C++ development and integration experiences, the choice is obvious. Names are good. Literals are bad. They hurt where it really hurts. :)

Attila a.k.a. WW

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #28 on: September 19, 2007, 06:31:32 pm »
must not obey ==> does not need to obey

Me speeky no Engelishe

Offline dempl_dempl

  • Multiple posting newcomer
  • *
  • Posts: 18
    • Stosha's Lnux Widgets
Re: NULL undefined
« Reply #29 on: September 22, 2007, 12:38:41 am »
Muahahahahahahaha :lol: :D

wwolf and killerbot:
You're both mad!!! mad!!!


wwolf : you're also biggest poseur. I know this guy bla bla bla ... I know that guy .. oooo ... look at me bla bla bla :p


« Last Edit: September 22, 2007, 12:40:53 am by dempl_dempl »

Offline wwolf

  • Multiple posting newcomer
  • *
  • Posts: 34
Re: NULL undefined
« Reply #30 on: September 22, 2007, 05:19:54 am »
Well, you know, everybody is good for something. If nothing else, one can serve as a bad example.

BTW if you had paid attention to a bit more than the (impossible to get from writing) emotions, and concentrated on meaning instead of (imagined) motivation, you would have seen that my references to those people were merely serving the purpose of showing that I know what I am talking about. Since it seemed that it was not a widely held view. :>

And if you carefully read my posts you will not find any place where I had asked anyone to look at me.  None of the posters here deserved that harsh punishment.  BTW you are the first one in my whole life who have called me a poser.  So you are either a genius who can see what nobody else can, or you are plain wrong. ;)  I can live with either. :>

Attila a.k.a. WW

Offline insolence

  • Single posting newcomer
  • *
  • Posts: 6
Re: NULL undefined
« Reply #31 on: September 22, 2007, 07:15:50 am »
Certainly whether or not NULL is defined is something that can be clarified by looking at the latest published standards.

But separate from this issue I would say that beginners should not be discouraged nor perhaps encouraged from using NULL or macros or anything else.

Admittedly I come from a C, not a C++ background, but its more important that your code be readable and understandable to you and those maintaining it, then that you adhere slavishly to one prominent programmer or another's advice.  If you use macros or NULL to do this, there's nothing wrong with that.

"gotos" in C are considered generally very ugly as they create code that is difficult to debug.  But there is STILL a use for them.  (C++ has try and catch to serve part of this need.)  But what I think should be most clear from this debate is that different people have different coding styles and a beginner must find their own.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: NULL undefined
« Reply #32 on: September 28, 2007, 11:54:51 am »
If I recall correctly, they are thinking about a *template* for something like nullptr, but I am not sure.
You're going to laugh... for some reason, I woke up this morning and had an implementation of nullptr in my mind.
It works exactly like it should, and it is so ridiculously easy that I just had to put it into prep.h :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: NULL undefined
« Reply #33 on: September 28, 2007, 11:59:11 am »
Ok, forgot about multiple definitions... will fix in a minute. :)

EDIT:
With the header being included a thousand times, it gets slightly more complicated, i.e. a .cpp file and an extern declaration are needed, but... it works :)


As a sidenote, doing a global file search for NULL, I noticed that NULL in Code::Blocks occurs in a lot of places where it is actually useless in my opinion, such as in all manager classes:
EditorColourSet* GetColourSet(){ return (this==NULL) ? 0 : m_Theme; }

Unless I have a gross misunderstanding, this cannot possibly be NULL, as all managers are singleton classes. Even if there was the possibility of calling a non-static member function on an uninitialised object, then this would not necessarily be NULL either (but some random, uninitialised value).
« Last Edit: September 28, 2007, 01:00:17 pm by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5215
Re: NULL undefined
« Reply #34 on: September 28, 2007, 03:32:03 pm »
@Thomas, don't forget to adjust both Cb cbp's and makefile.am for the added nullptr.cpp file

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: NULL undefined
« Reply #35 on: September 28, 2007, 04:04:43 pm »
Right, thank you... forgot the makefile as usual.   8)
Funnily, the project files aren't committed either, thought I had done that.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."