Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
您可以删除使用 new 运算符创建的内容。
字符串文字具有静态存储期限。他们一直活着直到程序结束。
在此代码片段中,
const char* s1 = "John"; const char* s2 = new char[] {"Cena"};
仅动态分配由字符串文字 "Cena" 初始化的数组。因此,要删除它(以释放分配的内存),您需要将运算符delete []应用于指针s2。
"Cena"
s2
delete [] s2;
You may delete what was created using the operator new.
String literals have static storage duration. They are alive until the program ends.
In this code snippet
there is allocated dynamically only the array initialized by the string literal "Cena". So to delete it (to free the allocated memory) you need apply the operator delete [] to the pointer s2.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(1)
您可以删除使用 new 运算符创建的内容。
字符串文字具有静态存储期限。他们一直活着直到程序结束。
在此代码片段中,
仅动态分配由字符串文字
"Cena"
初始化的数组。因此,要删除它(以释放分配的内存),您需要将运算符delete []应用于指针s2
。You may delete what was created using the operator new.
String literals have static storage duration. They are alive until the program ends.
In this code snippet
there is allocated dynamically only the array initialized by the string literal
"Cena"
. So to delete it (to free the allocated memory) you need apply the operator delete [] to the pointers2
.