[英]Freeing array of c-strings
Here I have an array of dynamically-allocated c-strings. 这里我有一个动态分配的c字符串数组。
What is the proper way to free such an array? 释放这样一个数组的正确方法是什么?
It is necessary to individually free each element of A
, as code below? 有必要单独释放A
每个元素,如下面的代码?
Thank you. 谢谢。
#include <string.h>
int main()
{
const char** A = new const char*[3];
A[0] = (const char*)memcpy(new char[5], "str0", 5);
A[1] = (const char*)memcpy(new char[5], "str1", 5);
A[2] = (const char*)memcpy(new char[5], "str2", 5);
// Is this the proper way to free everything?
for (int i = 0; i < 3; ++i)
delete[] A[i];
delete[] A;
}
Yes. 是。 Everything is OK as long as every new ...[]
is matched with a delete[] ...
. 只要每个new ...[]
与delete[] ...
相匹配,一切都会好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.