cppcheck:可能的空点取消引用

cppcheck : possible null point dereference

我的以下代码工作正常,但仍然 "cppcheck" 发出警告,

Either the condition 'mem_limit_str!=0' is redundant or there is possible null pointer dereference: mem_limit_str.

下面是代码,

 if (mem_limit_str!= NULL) //CPPCHECK error
                free(mem_limit_str);

谁能帮我解决这个问题?

您不需要在 freeing 之前检查空指针。你可以简单地说:

free(mem_limit_str);  // no if condition

此警告仅表明 if 条件是多余的。

来自 n1124 草稿

7.20.3.2 The free function

Synopsis

#include <stdlib.h> 
void free(void *ptr); 

Description

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. [...]

std::free 可以很好地与空指针一起使用。检查是多余的。

If ptr is a null pointer, the function does nothing.

顺便说一句:deletedelete[] 也是如此。