Jump to content
  • Advertisement
Sign in to follow this  
Vincent_M

NULL vs nullptr

This topic is 1319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to embrace C++-11's features this past year, and one thing I keep wondering about is when to use nullptr. I'm currently using it for all of my pointers everywhere. The problem is, if I'm using a pointer to a class/struct that's defined in a library, use functions/methods also defined in that library to create and destroy it, and it assigns NULL instead of nullptr, then should I be using NULL to initialize my pointers, and making the appropriate NULL-checks in general use instead of nullptr? I use a lot of libraries written in C, and built with a C compiler. These libraries won't even be able to use nullptr within the scope of their code-base let alone be built to use nullptr instead of NULL.

Share this post


Link to post
Share on other sites
Advertisement

There is a type safety aspect to nullptr.  See the example below

// Declarations
int func(int Input);
int func(int* Input);
 
func(0);
func(NULL);
func(nullptr);

Which version of the function gets called for each line?

Edited by Rattrap

Share this post


Link to post
Share on other sites

I believe SiCrane used to cite as an example that member function pointers on a popular compiler can be bigger than regular pointers, and have a non-zero bit pattern for the "null" value.

Share this post


Link to post
Share on other sites

I believe SiCrane used to cite as an example that member function pointers on a popular compiler can be bigger than regular pointers, and have a non-zero bit pattern for the "null" value.

Yes, that's a good example for another pointer/size pitfall. But note that it does not apply to the null pointer itself. A value of type nullptr_t is explicitly defined [3.9.1/10] to have a size of sizeof(void*). It can however be converted to a pointer to member (presumably changing its size!).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!