简体   繁体   English

如何在C ++中删除变量

[英]How can I delete variables in C++

I want to free ram in my program. 我想在我的程序中释放ram。

Even though I'm a novice, I really care about performance. 即使我是新手,我也非常关心性能。

string i_want_to_delete_this = "I don't want this cleared, but            
completely gone";
/* I figured out */ i_want_to_delete_this.clear() /* simply sets the
string value to "". I want to be able to do this with every
datatype! I want it completely gone, not even declared anymore. */

I don't see why you want to do this, and in any case you cannot delete or otherwise remove named variables, except in so far as they are conceptually removed for you by the compiler when they go out of scope, and actually removed when the function that contains them exits. 我不明白你为什么要这样做,并且在任何情况下你都不能删除或以其他方式删除命名变量,除非它们在概念上被编译器在超出范围时被你删除,并且实际上被删除了包含它们的函数退出。 For example: 例如:

{
   {
       string i_want_to_delete_this = "I don't want this cleared, but            
completely gone";
   }     // it's gone
}

There are 3 kinds of variables. 有3种变量。 Depending on the kind you manage the memory differently. 根据您管理内存的种类不同。

Global Variables 全局变量

These reside in a special section of your program. 它们位于程序的特殊部分。 They appear when the program starts and dissapear when the program ends. 它们在程序启动时出现,在程序结束时消失。 You cannot do anything to reclaim the memory occupied by global variables. 你无法做任何事情来回收全局变量占用的内存。 Some more complex constants may fall into that category as well. 一些更复杂的常量也可能属于该类别。 Your character string literal "I don't want this cleared, but completely gone" is most likely going to reside there, no matter if you copy it to i_want_to_delete_this variable or not. 您的字符串文字"I don't want this cleared, but completely gone"很可能会驻留在那里,无论您是否将其复制到i_want_to_delete_this变量。

Stack Variables 堆栈变量

Local variables and function arguments. 局部变量和函数参数。 They appear within your code. 它们出现在您的代码中。 The memory is allocated when you enter the scope of that variable, and are removed automatically when you leave the scope: 输入该变量的范围时会分配内存,并在离开范围时自动删除:

{ //beginning of the scope
    int foo = 42; // sizeof(int) bytes allocated for foo
    ...
} //end of the scope. sizeof(int) bytes relaimed and may be used for other local variables

Note that when optimizations are on, local variables may be promoted to registers and consume no RAM memory at all. 请注意,当启用优化时,可能会将局部变量提升为寄存器,并且根本不消耗RAM内存。

Heap Variables 堆变量

Heap is the only kind of memory you manage yourself. 堆是你自己管理的唯一一种记忆。 In plain C you allocate memory on the heap using malloc and free it with free , eg 在普通的C您在使用堆分配内存malloc与和释放它free ,如

int* foo = (int*)malloc(sizeof(int)); //allocate sizeof(int) bytes on the heap
...
free(foo); //reclaim the memory

Note that the foo itself is a local variable, but it points to a portion of heap memory where you can store an integer. 请注意, foo本身是一个局部变量,但它指向堆内存的一部分,您可以在其中存储整数。

Same think in C++ would look as: 同样认为在C ++中看起来像:

int* foo = new (int; //allocate sizeof(int) bytes on the heap
...
delete foo; //reclaim the memory

Heap is usually used when the variable must exist much longer than the scope, usually depending on some more complex program logic. 当变量必须比范围长得多时,通常使用堆,通常取决于一些更复杂的程序逻辑。

Automatic variables, those not using malloc or the new operator, are deleted when execution leaves a function or substatement. 当执行离开函数或子语句时,将删除自动变量,即不使用mallocnew运算符的变量。

Variables declared outside a function will remain in memory until the program terminates. 在函数外部声明的变量将保留在内存中,直到程序终止。

Also, I would focus on program correctness and robustness. 此外,我将专注于程序的正确性和稳健性。 Only worry about RAM or Memory usage if program doesn't fit in the memory of your platform. 如果程序不适合您的平台内存,则只担心RAM或内存使用情况。

In the real world, the workplace, most data that is processed by a program and doesn't fit into memory can be split into pieces and each piece processed separately (there are a few exceptions though). 在现实世界中,工作场所,由程序处理并且不适合存储器的大多数数据可以分成多个部分并且每个部分单独处理(尽管有一些例外)。

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

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