简体   繁体   中英

How can I delete variables in C++

I want to free ram in my program.

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. 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.

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.

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

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.

Same think in C++ would look as:

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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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