char* 是用“...”创建的,new 分别在栈和堆上吗?如果我的理解是对还是错,只想要一个简短的答案?

Is char* created with "..." and new are on stack and heap respectively? Just want a brief answer if my understanding is right or wrong?

const char* s1 = "John";
const char* s2 = new char[] {"Cena"};
// is s1 on stack?
// is s2 on heap?
...
// use s1 and s2
...

delete s1;
// do I need to delete s1?

delete[] s2?
// s2 definitely must be deleted right?

我已经在上面的评论中添加了我的问题。提前致谢。

您可以删除使用 operator new 创建的内容。

字符串文字具有静态存储持续时间。他们还活着,直到节目结束。

在此代码段中

const char* s1 = "John";
const char* s2 = new char[] {"Cena"};

仅动态分配了由字符串文字初始化的数组"Cena"。因此,要删除它(释放分配的内存),您需要将运算符 delete [] 应用于指针 s2.

delete [] s2;