+ 2
Are there any reason why using NULL in pointer ? Why not 0?
in Sololearn C++ Course , section dynamic memory, there is a code : int *p = NULL; // Pointer initialized with null why using NULL? why not using 0? i read on many sites, NULL is not safe because it has different definition between C and C++ even between different version of C++.
5 ответов
+ 4
As of C++11 prefer nullptr instead of NULL or 0.
http://stackoverflow.com/questions/13816385/what-are-the-advantages-of-using-nullptr
+ 3
Partially, it's about creating a standard practice. Safe use of pointers involves making sure they actually point at something before you use them, and initializing or resetting to NULL makes that both easier and distinct from mathematical calculations.
Another reason is that depending on your compiler/computer, 0 might actually be an accessible memory location, so you could get unpredictable behaviour as a result of a program that used 0 as the default.
+ 3
No @Setiawan, that's not what I meant. All of the informations contained in the sololearn course still applies.
But, yes, C++ is still evolving. There are many new features that may change the way C++ programmers code.
+ 2
You're free to use 0, I'm sure. I think NULL is just a macro in C and C++ that depends on your operating system's implementation of C and C++.
+ 2
@Ulisses Cruz
do you mean Soloelarn course is obsolate?
if yes, can we send suggestion to improve the course? but how?