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

14th May 2017, 2:57 AM
Setiawan Next
Setiawan Next - avatar
5 ответов
14th May 2017, 3:08 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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.
14th May 2017, 5:08 AM
Jim
Jim - avatar
+ 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.
14th May 2017, 12:07 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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++.
14th May 2017, 3:06 AM
Steven Schneider
Steven Schneider - avatar
+ 2
@Ulisses Cruz do you mean Soloelarn course is obsolate? if yes, can we send suggestion to improve the course? but how?
14th May 2017, 3:27 AM
Setiawan Next
Setiawan Next - avatar