Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Compiler flag equivalent to "-fno-delete-null-pointer-checks" ?


The oneAPI C++ compiler seems to default to the optimization of eliminating a "if (this == NULL)" test in a class member.  Previous Intel C++ compilers and the MS VS compiler do not make this optimization by default.


GCC has a "-fno-delete-null-pointer-checks" option to turn off this optimization.  Is there an equivalent flag for the oneAPI C++ compiler that would turn off this optimization?


I do understand that calling a member function on a null pointer is by standard "undefined", which is why this optimization exists, but we have existing code we are attempting to migrate which uses the prior Intel/MS functionality of allowing the member function call and making a nullptr test in the member itself.


Any ideas to turn off this optimization?


0 Kudos
1 Reply

I also have this exact issue.  When optimizations are off, program compiles and works properly.  When optimizations are on, the test of "this" pointer is optimized away and the program will crash.  

Here is some example code:

class Stuff {
int value;
int GetValue() { if (!this) return 0; else return value; }
Stuff(int i) { value = i; } // Constructor

The calling code to test would look like:

int x = 0;
Stuff *stuffptr = NULL;
x = stuffptr->GetValue(); // x should be zero
stuffptr = new Stuff(20);
x = stuffptr->GetValue(); // x is 20

When run without optimizations, the code works properly as per the comments above.  The "(!this)" test will catch the null function pointer and return zero.

When run with Intel optimizations, the first GetValue() will crash with a null pointer dereference.  The "(!this)" test was removed by optimization.

Compiling on MS Visual Studio works properly with all optimizations on.

As the original post noted, GCC has a flag specific for this optimization to be turned on or off.


0 Kudos