c语言取消对null指针的引用,c – 强制允许取消引用NULL指针

我有一个非常古老的(和巨大的)Win32项目,通过转换指针取消引用指针,使用NULL指针进行大量检查.像这样:

int* x = NULL; //somewhere

//... code

if (NULL == &(*(int*)x) //somewhere else

return;

是的,我知道这段代码很愚蠢,需要重构.但由于代码量很大,这是不可能的.现在我需要在Xcode的MacOS Sierra下编译这个项目,这会导致很大的问题…事实证明,在发布模式下(使用代码优化),条件以不正确的行为执行(因为解除引用NULL而被称为未定义的行为指针).

根据 this document for GCC

,有一个选项-fno-delete-null-pointer-checks,但是当启用O1,O2或O3优化时,它似乎不适用于LLVM.所以问题是:我如何强制LLVM 8.0编译器允许这样的解引用?

UPDATE.检查问题的真实工作示例.

//somewhere 1

class carr

{

public:

carr(int length)

{

xarr = new void*[length];

for (int i = 0; i < length; i++)

xarr[i] = NULL;

}

//some other fields and methods

void** xarr;

int& operator[](int i)

{

return *(int*)xarr[i];

}

};

//somewhere 2

carr m(5);

bool something(int i)

{

int* el = &m[i];

if (el == NULL)

return FALSE; //executes in debug mode (no optimization)

//other code

return TRUE; //executes in release mode (optimization enabled)

}

something(int): # @something(int)

pushq %rax

movl %edi, %eax

movl $m, %edi

movl %eax, %esi

callq carr::operator[](int)

movb $1, %al

popq %rcx

retq

something(int): # @something(int)

movb $1, %al

retq