Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are variables removed from memory in C++?

I've been using C++ for a bit now. I'm just never sure how the memory management works, so here it goes:

I'm first of all unsure how memory is unallocated in a function, ex:

int addTwo(int num)
{
    int temp = 2;
    num += temp;
    return num;
}

So in this example, would temp be removed from memory after the function ends? If not, how is this done. In C# a variable gets removed once its scope is used up. Are there also any other cases I should know about?

Thanks

like image 656
jmasterx Avatar asked Dec 10 '09 13:12

jmasterx


People also ask

How are variables stored in memory in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

Do unused variables take up memory?

Does an unused member variable take up memory? No (if it is "really" unused).

Do local variables get destroyed?

Local variable lifetime: A function's local variables exists only while the function is executing. When the function begins, its local variables and its parameter variables are created in memory, and when the function ends, the local variables and parameter variables are destroyed.

Which command is used to remove variables from memory?

The drop command is used to remove variables or observations from the dataset in memory.


2 Answers

In C++ there is a very simple rule of thumb:

All memory is automatically freed when it runs out of scope unless it has been allocated manually.

Manual allocations:

  • Any object allocated by new() MUST be de-allocated by a matching delete().
  • Any memory allocated by malloc() MUST be de-allocated by a matching free().

A very useful design pattern in C++ is called RAII (Resource Acquisition Is Initialization) which binds dynamic allocations to a scoped object that frees the allocation in its destructor.

In RAII code you do not have to worry anymore about calling delete() or free() because they are automatically called whenever the "anchor object" runs out of scope.

like image 70
Thorsten79 Avatar answered Oct 31 '22 18:10

Thorsten79


Here, temp is allocated on the stack, and the memory that it uses is automatically freed when the function exits. However, you could allocate it on the heap like this:

int *temp = new int(2);

To free it, you have to do

delete temp;

If you allocate your variable on the stack, this is what typically happens:

When you call your function, it will increment this thing called the 'stack pointer' -- a number saying which addresses in memory are to be 'protected' for use by its local variables. When the function returns, it will decrement the stack pointer to its original value. Nothing is actually done to the variables you've allocated in that function, except that the memory they reside in is no longer 'protected' -- anything else can (and eventually will) overwrite them. So you're not supposed to access them any longer.

If you need the memory allocated to persist after you've exited the function, then use the heap.

like image 33
int3 Avatar answered Oct 31 '22 16:10

int3