C++删除 C 字符串数组/其他类型的数组
[编辑]
好的,这是有道理的,谢谢 Sharptooth 和 CashCow。您无法删除分配为 const 的数据,这使得字符串文字不再成为问题。因此,如果我将初始化更改为如下所示:
char **groups = new char*[2];
char *s1 = new char[10];
char *s2 = new char[10];
char c1 = 'a';
char c2 = 'b';
for(int i = 0; i < 9; i++)
{
s1[i] = c1;
s2[i] = c2;
}
s1[9] = NULL;
s2[9] = NULL;
groups[0] = s1;
groups[1] = s2;
对 for 循环进行硬编码,使其仅迭代 i=1 和 i=2,则一切正常。
我注意到 int arraySize = sizeof arr / sizeof *arr; 似乎仅在使用 new[] 而不是本地分配数组时才起作用。这是因为我原来的 char ** groups; 变量衰减为指针,对吗?
现在我想知道,有没有办法判断数据是否是const?
[原件]
我知道数组和指针是邪恶的,还有向量和链表这些伟大的东西。
但是,在内存管理方面我是个新手,我感觉有点受虐狂。假设我创建了一个 C 字符串数组。我从这个问题和FAQ-Lite中知道您必须将 type a = new type[len];
与 delete[] a;
匹配。或者说我是这么认为的。
FAQ-Lite 在此处讨论了管理锯齿状数组,但他专注于矩阵,我不确定它是否适用于我正在做的事情。
这段代码对我来说有意义,但在 delete[] a;
上断言失败(在 Visual Studio 2008 上调试)。这有什么问题,我该如何完成这个任务?
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
// Initialize array of C-strings
char *groups[] = {"testing1", "testing2"};
// Sanity check
cout << groups[0] << endl;
cout << groups[1] << endl;
// Compute size
int arrsize = sizeof groups / sizeof groups[0];
cout << arrsize << endl;
for (int i = 0; i < arrsize; i++)
{
// Since each string is a char array, free string memory with delete[]
cout << "Deleting element #" << i << endl;
delete[] groups[i];
}
cout << "Freeing pointer array." << endl;
// Free the memory storing the pointers
delete[] groups;
return 0;
}
[EDIT]
Okay, so that makes sense, thank you sharptooth and CashCow. You can't delete data allocated as const, which makes string literals out of the question. So if I change my initialization to look like this:
char **groups = new char*[2];
char *s1 = new char[10];
char *s2 = new char[10];
char c1 = 'a';
char c2 = 'b';
for(int i = 0; i < 9; i++)
{
s1[i] = c1;
s2[i] = c2;
}
s1[9] = NULL;
s2[9] = NULL;
groups[0] = s1;
groups[1] = s2;
Hard code my for loop so that it will only iterate through i=1 and i=2, then everything works.
I noticed that the int arraySize = sizeof arr / sizeof *arr;
only seems to work when the array is allocated with new[] instead of locally. This is because my original char ** groups;
variable decays to a pointer, right?
Now I'm wondering, is there anyway to tell whether or not data is const?
[ORIGINAL]
I know that arrays and pointers are evil, and that there are these great things called vectors and linked lists.
But, I'm a newbie when it comes to memory management, and I'm feeling a little masochistic. Say I make an array of C-strings. I know from this question and the FAQ-Lite that you must match type a = new type[len];
with delete[] a;
. Or so I think.
FAQ-Lite talks about managing jagged arrays here, but he's focusing on matrices, and I'm not certain if it applies to what I'm doing.
This code makes sense to me, but fails an assertion (debugging on Visual Studio 2008) on delete[] a;
. What is wrong with this, and how do I accomplish this task?
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
// Initialize array of C-strings
char *groups[] = {"testing1", "testing2"};
// Sanity check
cout << groups[0] << endl;
cout << groups[1] << endl;
// Compute size
int arrsize = sizeof groups / sizeof groups[0];
cout << arrsize << endl;
for (int i = 0; i < arrsize; i++)
{
// Since each string is a char array, free string memory with delete[]
cout << "Deleting element #" << i << endl;
delete[] groups[i];
}
cout << "Freeing pointer array." << endl;
// Free the memory storing the pointers
delete[] groups;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试释放字符串文字 - 这是未定义的行为:
仅对
new[]
返回的指针调用delete[]
。You try to deallocate string literals - that's undefined behavior:
only call
delete[]
on pointers returned bynew[]
.文字字符串是类似“testing1”的表达式。它们的类型为 const char *。它们无法修改,也无法删除。实际文本通常位于程序的静态只读内存区域中。
您可以构造一个可写的 char 数组,如下所示:
char group[] = "testing1";
这是可修改的,但您不能删除它,也不能扩展它以使其更大。它还具有本地作用域,因此您无法从函数返回它。
Literal strings are expressions like "testing1". They are of type const char *. They cannot be modified and they cannot be deleted. The actual text usually sits in the static read-only memory area of the program.
You can construct a char array that is writable like this:
char group[] = "testing1";
That is modifiable but you cannot delete it, nor can you extend it to make it bigger. It also has local scope so you cannot return it from a function.