简体   繁体   English

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

[英]How do you delete variables in c++?

How do you delete variables in a c++ program? 如何删除c ++程序中的变量? i have a simple int list[10]; 我有一个简单的int list[10]; and i want delete it and change it to int list[9] . 我想删除它并将其更改为int list[9] i would use vectors, but i still want to know how to do it 我会使用矢量,但我仍然想知道如何做到这一点

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::vectorstd::list等而不是使用原始C样式数组几乎总能更好。

This is not called deleting a variable but instead redefining the type of a variable. 这不是称为删除变量,而是重新定义变量的类型。 This is simply not possible in C++. 这在C ++中是不可能的。 Once a variable type has been established it cannot be changed. 一旦建立了变量类型,就无法更改。

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 list to an int* and allocate it's memory on the heap. list切换到int*并在堆上分配它的内存。 This will have the effect of redefining it to a different size without changing it's type. 这将具有将其重新定义为不同大小而不改变其类型的效果。

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. 没有“删除”在堆栈上声明的局部变量的概念。

To delete an array in C++ you'd use the delete[] operator. 要在C ++中删除数组,您可以使用delete[]运算符。 See this link . 看到这个链接

You'll want to be careful. 你要小心。 What you have is a static array. 你拥有的是一个静态数组。 When you use delete or delete[] it must be on dynamically allocated variables (ones you made with new ). 当你使用deletedelete[]它必须是动态分配的变量(你用new制作的变量)。 To do that: 要做到这一点:

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

Don't forget to delete if you new ! 如果你是new别忘了delete (Ideally you should use something managed so you can't forget, like you said). (理想情况下,你应该使用管理的东西,所以你不能忘记,就像你说的那样)。

Your int list[10]; 你的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. 声明为变量的数组是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: 虽然它仍然会声明一个声明,但它不是:新的操作数将触发mem分配代码)...如果你想分配更多或更少的空间,你需要先释放它:

delete[] list;

and then use the new operand to make a new on (of the same int type) 然后使用新的操作数来创建一个新的(相同的int类型)

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. 无法想象为什么你会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM