Use of NULL vs nullptr in UE4 C++

Dear Friends at Epic,

Expert coder Neil Griffth recently wrote this:

“You should really try to avoid using
NULL if possible and instead use
nullptr. It’s a lot safer if only
because you’re not checking against a
number (zero) - and it’s part of the
C++ language as of C++11 (which is
required by Rocket in any case!).”

#My Question

I was wondering if NULL in UE4 C++ still uses the actual macro, or if Epic has done this definition somewhere:

#define NULL  nullptr

#Summary

Anyone with any comments on NULL vs nullptr, feel free to post.

I am not taking a stance on this, I am asking for more info :slight_smile:

Though I do want to say, Thanks Neil for bringing this to my attention!

Rama

Expert coder Neil Griffth recently
wrote this:

Yep, he truly is … lucky us :slight_smile:

hee hee! :slight_smile:

To answer your question, I think NULL is a Microsoft thing; IIRC it’s defined in Windows.h, which is probably getting included somewhere along the line.

I doubt it’s #defined anywhere. It’s part of C++ now.

But yes, nullptr is most definitely preferred IMO :slight_smile:

neat!

The .h files of UE4 mostly use NULL, I found about 30 references in the .h to nullptr

I’m just curious if Epic has made them internally the same by

#define NULL nullptr 

or not

It’s unlikely that they did that - if they did that then it could break code for people who are using NULL as a value. :slight_smile:

Well, thanks again for shedding light on this subject Neil!

#:heart:

Rama

Just to clarify, you can check with:

int value1 = NULL;
int value2 = nullptr;

The first compiles, but not the second, meaning NULL is still defined to 0.

oooh great insight Andrew!

Thanks Andrew!

That fully answers this thread :slight_smile:

Thanks everyone, for the info!

#:heart:

Rama

Found the answer on the coding standard page :

nullptr should be used instead of the
C-style NULL macro in all cases. One
exception to this is that nullptr in
C++/CX builds (such as for Xbox One)
is actually the managed null reference
type. It is mostly compatible with
nullptr from native C++ except in its
type and some template instantiation
contexts, and so you should use the
TYPE_OF_NULLPTR macro instead of the
more usual decltype(nullptr) for
compatibility.

This is an old thread, but I thought I’d mention that as of 4.25.3, the Third Person Template from Epic still uses !=NULL repeatedly in the character CPP
If that’s not a good practice, they should probably update their code samples to keep people from misunderstanding it.

Example:

class AMyDefaultClass* theDefaultClass;

****//the class was exist & IsValid(), So class is not NULL

if(theDefaultClass != NULL) UE_LOG(LogTemp, Warning, TEXT("AMyDefaultClass is not NULL"));

//the class IsValid() but you not able to use class data(variable, function(), etc) with public otherwise crash editor, So class was nullptr

if(theDefaultClass == nullptr)  UE_LOG(LogTemp, Warning, TEXT("AMyDefaultClass was nullptr"));