C++ “删除”的数组形式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/860447/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:37:54  来源:igfitidea点击:

What is the array form of 'delete'?

c++arraysmemory-management

提问by RBerteig

When I compiled a code using the array name as a pointer, and I deleted the array name using delete, I got a warning about deleting an array without using the array form (I don't remember the exact wording).

当我使用数组名称作为指针编译代码,并使用 删除数组名称时delete,我收到一条关于不使用数组形式删除数组的警告(我不记得确切的措辞)。

The basic code was:

基本代码是:

int data[5];
delete data;

So, what's the array form of delete?

那么,delete 的数组形式是什么?

回答by RichieHindle

The array form of delete is:

delete的数组形式为:

delete [] data;

Edit:But as others have pointed out, you shouldn't be calling deletefor data defined like this:

编辑:但正如其他人指出的那样,您不应该调用delete这样定义的数据:

int data[5];

You should only call it when you allocate the memory using newlike this:

您应该只在使用以下方法分配内存时调用它new

int *data = new int[5];

回答by RBerteig

You either want:

你要么想要:

int *data = new int[5];
... // time passes, stuff happens to data[]
delete[] data;

or

或者

int data[5];
... // time passes, stuff happens to data[]
// note no delete of data

The genera rule is: only apply deleteto memory that came from new. If the array form of newwas used, then you mustuse the array form of deleteto match. If placement newwas used, then you either never call deleteat all, or use a matching placement delete.

一般规则是:仅适用delete于来自new. 如果使用的是数组形式new,则必须使用数组形式delete进行匹配。如果使用了放置new,那么您要么根本不调用delete,要么使用匹配的放置delete

Since the variable int data[5]is a statically allocated array, it cannot be passed to any form of the deleteoperator.

由于变量int data[5]是静态分配的数组,因此不能传递给任何形式的delete运算符。

回答by DannyT

As the other have said, you must use the vector form of delete:

正如其他人所说,您必须使用删除的向量形式:

void some_func(size_t n)
{
  int* data = new int[n];

  . . . // do stuff with the array

  delete [] data; // Explicitly free memory
}

Be very wary of this, because some compilers will notwarn you.

对此要非常小心,因为有些编译器不会警告您。

Even better, there is very rarely any need for using vector new/delete. Consider whether your code can be altered to make use of std::vector:

更好的是,很少需要使用 vector new/delete。考虑是否可以更改您的代码以使用 std::vector:

void some_func(size_t n)
{
  std::vector<int> data(n);

  . . . // do stuff with the array

} // memory held by data will be freed here automatically

And if you are dealing with the memory in a local scope, consider using STLSoft's auto_buffer, which will allocate from an internal buffer (held on the stack, as part of the instance) if possible, only going to the heap if it cannot:

如果您正在处理本地范围内的内存,请考虑使用STLSoftauto_buffer,如果可能,它将从内部缓冲区(保存在堆栈中,作为实例的一部分)分配,只有在它不能时才转到堆:

void some_func(size_t n)
{
  stlsoft::auto_buffer<int, 10> data(n); // only allocates if n > 10

  . . . // do stuff with the array

} // memory held by data will be freed here automatically, if any was allocated

Read more about auto_buffer.

阅读有关 auto_buffer 的更多信息

回答by Nikolai Fetissov

The code as shown has the array either on the stack, or in initialized part of the data segment, i.e. you don't deallocate it (which, as mentioned by others, would be "undefined behavior".) Were it on the "free store", you'd do that with delete [] data.

所示的代码在堆栈上或数据段的初始化部分中有数组,即您不释放它(正如其他人所提到的,这将是“未定义的行为”。) store”,你可以用delete [] data.

回答by Sergio

Just as RichieHindle stated above when you want to free the space dynamically allocated for an array pointed by datayou have to put two brackets []between the reserved word deleteand the pointer to the beginning of the allocated space. Since datacan point to a single intin memory as well as to the first element in the array this is the only way you let the compiler know that you want to delete the whole chunk of memory. If you don't do it the proper way the behaviour is "undetermined" (Stroustrup, The C++ Programming Language).

正如 RichieHindle 上面所说的,当你想要释放为一个数组动态分配的空间时,data你必须[]在保留字delete和指向分配空间开头的指针之间放置两个括号。由于data可以指向int内存中的单个元素以及数组中的第一个元素,这是您让编译器知道您要删除整个内存块的唯一方法。如果您不以正确的方式执行此操作,则行为是“未确定的”(Stroustrup,C++ 编程语言)。