thats not easier then including <iostream>.. im asking why is 'cstddef' 'better'
thats not easier then including <iostream>.. im asking why is 'cstddef' 'better'
Using <cstddef> is better because that is where the standard says that NULL is defined. If you use iostream, it may work for you, but it might not work when you switch compilers, it might not work if you give your code to somebody else, and it might not work if you post code here for somebody to help you with and they try to compile it on their compiler.
Of course, it really doesn't matter because unless you are writing code that needs to work in C also, you should just use 0.
Probably because it is not standard for iostream to include the file that has the definition of
NULL. I don't feel like digging in the standard to make sure, but this is most likely the case. Which means this may or may not work on all compilers.
EDIT: Pwned by Daved.
ive never had to use cstddef so since i usually have iostream already included i dont have to do anything else.. if i ever need NULL and dont have/need iostream then as daved said earlier 0 is the easiest and most portable way.
Including cstddef and using NULL is more readable in some cases, and same portability. Using 0 and NULL is the same, but using NULL just by including <iostream> is non standard. Stick to the standard when you can, makes life easier for everyone, besides the extra effort of typing out #include <cstddef> is nothing compared to debug time in a programs lifetime.
rather than having to include a whole header you dont need, would it also be ok to just #define it yourself?
No, because you could easily get multiple definition warnings that are extremely annoying.
but if i dont have a header file that has defined it previously, ie if i dont #include<cstddef> then its not defined, so if i use #define for NULL then itd be the first time the compiler would 'see' it... no?
You'd what to put include gaurds in. Header files are ment to work with more than one sourse file, so just because one sourse file doesn't define null, dosen't mean another will.
You'd have to do this:Code:#ifndef NULL #define NULL ((void *) 0) #endif
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
This is C++ NULL should be defined as 0... see my above post (near the start of this thread).
i was looking in the stddef.h file (where this is actually defined) and it looks like they have both defined, so he just missed the #define for the value of 0.
edit: oh i see what your saying, for C++ the standard is 0 but for C its ((void*)0)Code:/* Define NULL pointer value */ #ifndef NULL #ifdef __cplusplus #define NULL 0 #else /* __cplusplus */ #define NULL ((void *)0) #endif /* __cplusplus */ #endif /* NULL */
Right, I was just making sure that he used the right one, the one he posted is the C style.
Of course, why would you go through any of that trouble when you should just use 0 anyway.
Luckily, in a few years everybody will start using nullptr instead so at hopefully at some point it will be consistent.