When a function ends are its local variables deallocated?

10,554

Solution 1

All objects have an associated storage duration. A storage duration describes how long the storage for an object is kept around. Local variables that are not references introduce an object with automatic storage duration, which means that the storage of those objects is automatically destroyed at the end of its scope.

Reference type variables do not introduce an object and may not even require storage of their own, but they still have storage duration (§3.7/3). If a reference does require storage, it will be released according to the reference's storage duration.

As such, any kind of local variable declaration will not leak. In fact, you cannot delete an object with automatic storage duration. That is only used for objects with dynamic storage duration, which are allocated using new.

Solution 2

The local variables that are allocated on stack, i.e. without using memory allocation functions or operators like malloc and new, are deleted automatically. All other variables have to be deleted by using delete as they are stored on heap.

Solution 3

If you manually allocate memory you must deleted it whenever you need,

Example:

char* foo()
{
    char* manually_allocated_char  = new char(); // this will 'live' outside the function
    char  autamically_allocated    = 'a'; // this will be 'deleted'
    return manually_allocated_char;
}


void main()
{
    char* a_new_char = foo();
    delete a_new_char; // You must free memory you have allocated for not having memory leaks
}

Solution 4

Dynamically allocated memory using malloc, realloc, new, and new[] must be deleted. These are in heap memory. Others would get automatically deallocated.

Share:
10,554
user3169700
Author by

user3169700

Updated on June 18, 2022

Comments

  • user3169700
    user3169700 almost 2 years

    If not does that mean that I would have to end each function by deleting all local variables if I wanted to prevent 100% of memory leak?

  • Kerrek SB
    Kerrek SB about 10 years
    This still leaves it open what happens to reference variables. They are not objects, but they also cause no leeks.
  • James Kanze
    James Kanze about 10 years
    @KerrekSB Neither pointers nor references cause leeks; only dynamic allocation can cause a leek. But if you write things like MyType& var = *new MyType;, you can leak with a reference as well.
  • jia103
    jia103 about 10 years
    Also note that when using smart pointers or auto_ptr, you're still allocating dynamic memory on the heap using new, but the clean-up is managed for you automatically.
  • jia103
    jia103 about 10 years
    Note that this example is good for illustration only; try not to strive for writing code this way normally. In general, try to keep your news and deletes together. If you can't do that and absolutely must do something like this, then comment comment comment!
  • Kerrek SB
    Kerrek SB about 10 years
    @JamesKanze: I realize that. I was referring to the space taken up by var itself. The answer doesn't seem to cover it, since var is not an object.
  • Kerrek SB
    Kerrek SB about 10 years
    @JamesKanze: In particular, I would like to see a reference to 3.7/3.
  • Joseph Mansfield
    Joseph Mansfield about 10 years
    @KerrekSB Added a paragraph on that. Thanks for highlighting it.
  • James Kanze
    James Kanze about 10 years
    @KerrekSB OK. I hadn't realized that you were talking about the memory of the reference: references may or may not require memory, and it's up to the compiler to ensure that any memory they may require never leaks (unless it is part of a larger object which is leaked).
  • Matheus Rocha
    Matheus Rocha almost 8 years
    In this case, what is the real advantage of using malloc or new?
  • Matheus Rocha
    Matheus Rocha almost 8 years
    What about static variables? They survive the end of the scope. Must I delete them when they're no longer needed?
  • Netwave
    Netwave almost 8 years
    @MatheusRocha, it depends, if you want the function to "remember some state" better to leave it as it is (if you use new operator), for other statically allocated variables without using new it is not necesary.
  • Ari Seyhun
    Ari Seyhun over 6 years
    Simply explained. Thank you!