小编Abh*_*Jha的帖子

C++ 中的警告:尝试分配新的 int32_t 时必须检查指针持有的值

我试图学习 C++ 中的动态内存分配。我的程序可以编译并运行,但 Visual Studio 向我抛出这些警告。

他们的意思是什么?

Warning C28193  'ptr' holds a value that must be examined.
Warning C28182  Dereferencing NULL pointer. 'ptr' contains the same NULL value as 
'new(1*4, nothrow)'
Run Code Online (Sandbox Code Playgroud)

我的代码:

#include <iostream>
#include <cstdint>

int main()
{
    int* ptr = nullptr;

    if (!ptr) {
        ptr = new (std::nothrow) int32_t;
        *ptr = 10;
    }

    std::cout << *ptr << "\n";

}
Run Code Online (Sandbox Code Playgroud)

c++ warnings pointers new-operator dynamic-memory-allocation

1
推荐指数
1
解决办法
858
查看次数