我正确使用新操作员吗?

App*_*rew 2 c++ new-operator

我有以下指针.

char **x = NULL;
Run Code Online (Sandbox Code Playgroud)

x将指向一个指针数组.以下代码是否正确?

x = new (nothrow) (*char)[20];
Run Code Online (Sandbox Code Playgroud)

我们将使用它来处理它

delete[] x;
Run Code Online (Sandbox Code Playgroud)

x = (char **) malloc(sizeof(char **) * 20);
Run Code Online (Sandbox Code Playgroud)

x = new (nothrow) (*char)[20];
Run Code Online (Sandbox Code Playgroud)

当量?

xto*_*ofl 7

除了unwind提到的指针语法之外,它是等价的:在两种情况下都将分配和删除20个char*的数组.

C++ - 熟练的警告:std::vector< std::string > 改为使用:)不需要内存管理.