相关疑难解决方法(0)

免费后将变量设置为NULL

在我的公司中,有一个编码规则,在释放任何内存后,将变量重置为NULL.例如 ...

void some_func () 
{
    int *nPtr;

    nPtr = malloc (100);

    free (nPtr);
    nPtr = NULL;

    return;
}
Run Code Online (Sandbox Code Playgroud)

我觉得,在上面显示的代码中,设置为NULL没有任何意义.或者我错过了什么?

如果在这种情况下没有任何意义,我将采用"质量团队"来删除此编码规则.请指教.

c malloc free coding-style heap-memory

146
推荐指数
11
解决办法
8万
查看次数

为什么不为C++提供DELETE宏的原因

有没有什么好的理由(除了"宏是邪恶的",也许)不使用以下宏?

#define DELETE( ptr ) \
if (ptr != NULL)      \
{                     \
    delete ptr;       \
    ptr = NULL;       \
}

#define DELETE_TABLE( ptr ) \
if (ptr != NULL)            \
{                           \
    delete[] ptr;           \
    ptr = NULL;             \
}
Run Code Online (Sandbox Code Playgroud)

c++ macros pointers memory-management

17
推荐指数
6
解决办法
8826
查看次数