C++ 你如何删除C++中的变量?

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

How do you delete variables in c++?

c++arrays

提问by fried_rice

How do you delete variables in a c++ program? i have a simple int list[10];and i want delete it and change it to int list[9]. i would use vectors, but i still want to know how to do it

你如何删除C++程序中的变量?我有一个简单的int list[10];,我想删除它并将其更改为int list[9]. 我会使用向量,但我仍然想知道怎么做

回答by Kristopher Johnson

If you have something declared like this:

如果你有这样的声明:

int list[10];

there is no way to "delete" it. It is either a global variable, with statically allocated storage, or it is a local variable with storage on the stack.

没有办法“删除”它。它要么是一个全局变量,具有静态分配的存储空间,要么是一个存储在堆栈上的局部变量。

I don't know exactly what you are trying to accomplish, but maybe something like this will help you figure it out:

我不知道你到底想完成什么,但也许这样的事情会帮助你弄清楚:

int *list = new int[10];  // allocate a new array of 10 ints
delete [] list;           // deallocate that array
list = new int[9];        // allocate new array with 9 ints

As you suggest in your question, you will almost always be better off using std::vector, std::list, and the like rather than using raw C-style arrays.

正如您在问题中所建议的那样,使用std::vector, 等几乎总是std::list比使用原始 C 样式数组更好。

回答by JaredPar

This is not called deleting a variable but instead redefining the type of a variable. This is simply not possible in C++. Once a variable type has been established it cannot be changed.

这不称为删除变量,而是重新定义变量的类型。这在 C++ 中根本不可能。一旦建立了变量类型,就不能再更改它。

There are several ways to emulate or work around this behavior ...

有几种方法可以模拟或解决此行为......

  • Create a child scope and define a new variable of the same name with a different type.
  • Switch listto an int*and allocate it's memory on the heap. This will have the effect of redefining it to a different size without changing it's type.
  • 创建一个子作用域并定义一个不同类型的同名新变量。
  • 切换list到 anint*并在堆上分配它的内存。这将具有将其重新定义为不同大小而不改变其类型的效果。

回答by Greg Hewgill

If your array is allocated on the heap, then:

如果您的数组分配在堆上,则:

int *L = new int[10];
delete[] L;
L = new int[9];

There is no concept of "deleting" a local variable declared on the stack.

没有“删除”在堆栈上声明的局部变量的概念。

回答by Chris Pfohl

To delete an array in C++ you'd use the delete[]operator. See this link.

要在 C++ 中删除数组,您将使用delete[]运算符。请参阅此链接

You'll want to be careful. What you have is a static array. When you use deleteor delete[]it must be on dynamically allocated variables (ones you made with new). To do that:

你会想要小心。你拥有的是一个静态数组。当您使用deleteor 时,delete[]它必须位于动态分配的变量上(您使用 制作的变量new)。要做到这一点:

int* list = new int[10];
//...Code...
delete[] list;
list = new int[9];

Don't forget to deleteif you new! (Ideally you should use something managed so you can't forget, like you said).

别忘了delete如果你new!(理想情况下,您应该使用管理过的东西,这样您就不会忘记,就像您说的那样)。

回答by jpinto3912

Your int list[10];is a static one. Arrays that are declared as variables are static objects in C++, and you can't delete/create them... it's a compile time thing.

你的 int list[10];是一个静态的。声明为变量的数组是 C++ 中的静态对象,您不能删除/创建它们……这是编译时的事情。

You need to explicitly declare your array as an array object, say

您需要将数组显式声明为数组对象,例如

int *list=new int[10];

Although it still seams a declaration, it's not: the new operand will trigger mem-allocation code)... If you want to allocate more or less space you need to free it first:

虽然它仍然是一个声明,但它不是:新操作数会触发内存分配代码)...如果你想分配更多或更少的空间,你需要先释放它:

delete[] list;

and then use the new operand to make a new on (of the same inttype)

然后使用新的操作数在(相同int类型的)上创建一个新的

回答by Edward Strange

Of course, you could always do this:

当然,你总是可以这样做:

{
  int list[10];
  ... 'list' has 10 elements ...

  {
    int list[9];
    ... 'list' has 9 elements ... other 'list' inaccessible
  }
  ... 'list' 10 elements ... other 'list' non-existent.
}

Can't imagine why you would though.

无法想象为什么你会。