C++ 删除字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/763520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting a char array
提问by DaClown
This question is related to this one. Given this code:
这个问题和这个有关。鉴于此代码:
char *p = new char[200];
delete[] p;
what would happen if you set p[100] = '\0'
before deleting p?
如果p[100] = '\0'
在删除 p 之前设置会发生什么?
I had some code where I got a debug error when I tried to delete a not null-terminated char array, something about deleting heap memory that's not assigned. It seemed to delete memory out of the array's bounds.
我有一些代码,当我尝试删除非空终止字符数组时出现调试错误,这是关于删除未分配的堆内存。它似乎删除了数组边界之外的内存。
回答by
The code:
编码:
char *p = new char[200];
p[100] = 'struct A { P *p; A() { p = new P; } ~A() { delete p; } };
A getA() { return A(); } int main() { A a = getA(); }
';
delete[] p;
is perfectly valid C++. delete does not know or care about null-terminated strings, so your error must have had some other cause.
是完全有效的 C++。delete 不知道或不关心以空字符结尾的字符串,因此您的错误一定有其他原因。
回答by Johannes Schaub - litb
Nothing special would happen. You would write at some place in the middle of the allocated memory (100 bytes apart from the start, 99 bytes before the end of the allocated memory).
不会有什么特别的事情发生。您将在分配的内存中间的某个位置写入(距开始处 100 个字节,分配的内存结束前 99 个字节)。
Then you would free that allocated memory. The compiler will handle it exactly as we would expect. The memory allocated by that is completely unrelated to null terminated strings. You could stick everything you want into that memory. It's some "raw" chunk of storage, you could even create some arbitrary C++ object into that memory (placement new).
然后你将释放分配的内存。编译器将完全按照我们的预期处理它。由它分配的内存与空终止字符串完全无关。你可以把你想要的一切都塞进那个记忆里。这是一些“原始”存储块,您甚至可以在该内存中创建一些任意的 C++ 对象(新位置)。
Your bug is somewhere else. For example, some common error is this one, where the constructor is called once, but the destructor is called twice, double-deleting something:
你的错误在别处。例如,一些常见的错误是这样的,其中构造函数被调用一次,但析构函数被调用两次,双重删除某些东西:
##代码##Now, what happens is that the default constructor is called once, and the created object is copied zero or more times. But the destructor is run for each copy that's created. Thus, you will call the destructor on the pointer more than once, leading to such strange bugs. The correct way to fix thatis to use a smart pointer, like shared_ptr
. As an exercise, you can also do it without, by writing proper copy constructors that copy the object over and allocate memory in the copy constructor, so that the copy and the respective original object keep distinct pointers.
现在,默认构造函数被调用一次,创建的对象被复制零次或多次。但是析构函数是为每个创建的副本运行的。因此,您将不止一次调用指针上的析构函数,从而导致此类奇怪的错误。解决这个问题的正确方法是使用智能指针,例如shared_ptr
. 作为练习,您也可以不这样做,通过编写适当的复制构造函数来复制对象并在复制构造函数中分配内存,以便副本和各自的原始对象保持不同的指针。
回答by JaredPar
I think you're confusing a plain old char array with a char array representing a C style string. The C++ delete operator cares nothing for a C style string array. All it will ever see is an array of chars. It's really no different than say deleting an array of int's.
我认为您将普通的旧字符数组与表示 C 样式字符串的字符数组混淆了。C++ 删除运算符不关心 C 风格的字符串数组。它所看到的只是一个字符数组。这与删除一个 int 数组真的没有什么不同。
The presence or absence of the null terminator is only relevant in functions that treat a char*
as a C style string.
空终止符的存在与否仅与将 achar*
视为 C 样式字符串的函数有关。
回答by aJ.
It shouldn't matter. delete[] should be used to delete dynamically allocated array irrespective of its content.
应该没关系。delete[] 应该用于删除动态分配的数组,而不管其内容。
回答by Dima
This should work just fine, as far as I can tell. You allocate a chunk of memory, and the OS should keep track of it, and be able to deallocate it when asked. It should not matter at all what values you put into the buffer you've allocated.
据我所知,这应该可以正常工作。您分配了一块内存,操作系统应该跟踪它,并能够在被要求时释放它。将什么值放入已分配的缓冲区中根本无关紧要。
Sticking a NULL in the middle of a character array would certainly interfere with the C string functions such as strcmp
, strlen
, etc. but that is another matter entirely.
在字符数组的中间粘附一个NULL肯定会与C字符串功能,诸如干扰strcmp
,strlen
等,但其完全是另一回事。
回答by Joey
As others have noted, the code you've posted is perfectly valid and shouldn't cause any trouble.
正如其他人所指出的,您发布的代码完全有效,不会造成任何麻烦。
The error might have been caused by changing the value of p
somewhere in between.
该错误可能是由更改p
介于两者之间的某个值引起的。