Matthijn
Matthijn

Reputation: 3234

Why does dynamic memory not get cleaned when it is out of scope

I've just started playing with C++ (familiar with other languages) and I was wondering something.

When creating a dynamic sized array for example with new, why does it not get removed when it is out of scope, but an array with a fixed size does get removed?

E.g with the following code:

int foo()
{
    int baz[5]; // Gets removed out of scope
    int *bar = new int[5]; // Does not get removed, becomes a leak
}

Since both baz and bar are a pointer to the beginning of an array, can't the "runtime environment" apply the same technique (detect if something is out of scope, and delete it when it does) to something with a fixed size as a dynamic size?

What is the difference that it cannot do such a thing.

Upvotes: 1

Views: 1018

Answers (5)

user395760
user395760

Reputation:

The thing that does out of scope is one pointer to the memory, not the memory itself (it doesn't have a scope, just a life time). You wouldn't want to free memory whenever any pointer to it dies - virtually always you want to hand out temporary pointers (e.g. pass them to other functions), return a pointer from a function, store a copy of the pointer in some other places, etc.

In other words, in the course of using dynamically allocated memory you'd necessarily create and destroy many, many pointers to that memory. You wouldn't want most of those pointers to take the memory with them, only the last pointer should be used to free the memory. Any earlier and you get dangling pointers; any later and you don't have a pointer any more and can't free it at all.

There are ways to couple memory mangement to the life of a pointer, including the ability to transfer ownership: Returning the pointer, moving it from one variable to another, etc. This is a so-called smart pointers, specifically unique_ptr.

However, this is not suitable as the only pointer type, you still need non-owning pointers for all the aforementioned uses (handing out temporary referenced without freeing the memory afterwards). In addition, this idea (or at least its adoption into the mainstream) predates C++, as far as I know. It definitely predates C, which means C++ would need to keep raw pointers even if Bjarne had been born with innate mastership of smart pointers.

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254471

When creating a dynamic sized array for example with new, why does it not get removed

Because that is the main purpose of new: to allow you to control when the object is destroyed, which might be outside the scope of its creation.

(Dynamic allocation is also needed in other cases - to create arrays whose size or objects whose type isn't known at compile time, or which might be too big for the stack - since the language doesn't directly provide separate mechanisms for these cases. In that case, you can use a smart pointer or container to bind the objects' lifetimes to a scope.)

Since both baz and bar are a pointer to the beginning of an array

No they're not. baz is an array; it's destroyed when it goes out of scope. bar is a pointer to an array; it's destroyed, but the array it points to isn't.

can't the "runtime environment" apply the same technique

No. In general, there's no way to know whether a pointer points to a dynamic object. Even if there were, there's no way to know whether there are other references to it which should remain valid after this pointer is destroyed.

Upvotes: 2

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

Dynamic memory is not deallocated automatically at the end of scope because the very purpose of dynamic memory is to have more control. You may want to deallocate at the end of the same scope where it was allocated, but you may want to deallocate somewhere else - it all depends on the design of your program.

Upvotes: 1

Linblow
Linblow

Reputation: 479

Because the fixed size array is allocated in the function's stack and the dynamic one is allocated in the program's heap. The function stack is the memory space used to store your local variables. The stack is automatically free'd when the function end is reached. The heap is different, it's free'd once the program is closed.

Upvotes: 1

utnapistim
utnapistim

Reputation: 27365

Why does dynamic memory not get cleaned when it is out of scope

Because the purpose of dynamic memory is to be allocated past the end of scope. Dynamic memory is used when you need to allocate memory in one scope, then use and de-allocate it in another scope.

Upvotes: 7

Related Questions