Author Topic: NULL undefined  (Read 24465 times)

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 »