简体   繁体   中英

What happens when Memory is allocated using a function?

When you return a newly allocated variable through a function , is a copy made and passed and the original deleted automatically?

Im assuming theres no memory leak

#include <iostream>

using namespace std;

int* allocater()
{

    int* x = new int(1);
    return x; 

    // what happens to the memory allocated to x ?
}


int main()
{


int* a = allocater();
int* b = allocater();


cout<<*a<<"  "<<*b;

delete a;
delete b;

// all memory allocated has been deleted?

}

the output is as expected.

When you return a newly allocated variable

Objects with dynamic storage are not variables.

There is a variable in the function. It's named x . The type of the variable x is int* ie it is a pointer to an integer.

is a copy made and passed and the original deleted automatically?

x is an automatic variable so it is destroyed automatically when it goes out of scope. The variable is indeed copied as a return value into the calling expression - although, it is possible for the compiler to elide this copy, if it performs named return value optimisation. That is useful if the type is big, or slow to copy (which a pointer isn't).

The objects with dynamic storage (whose type is int in your program) are not destroyed automatically. They must be deallocated with a delete expression.

Whenever memory is allocated like this, it is reserved on "The Heap". This is an area of memory allocated to your program by the operating system. By using the allocation functions in C++, eg: new() or malloc() (there are others too), a contiguous block of this heap is reserved, and the address of it (a pointer) is returned to the calling code.

So in your function:

int *allocater()
{
    int *x = new int(1);
    return x; 
}

A single integer-sized piece of memory is reserved (probably 4-8 bytes), and the address of this memory is returned. This address is just a number, when the number is interpreted as a location in memory, it's called a pointer - but it's still just a number.

So when your function returns, the memory is still allocated on the heap. If your program forgets that number, this memory is "leaked" - you program cannot de-allocate it with delete() , delete[]() or free() because you don't have the number to tell the de-allocation function where to free.

In your code, because you store the return value from allocater() , it's possible to de-allocate the block with delete . So you code works fine, and the memory is de-allocated properly.

Nothing happens until you free it. Yep, that's a valid code and there are no leaks.

In C++11 (or earlier auto_ptr -s) there were introduced RAII pointers: unique_ptr , shared_ptr , etc. So if you use them:

int* allocater()
{
    auto x = std::make_unique<int>(5);
    return x.get();  // getting raw ptr
}

it becomes invalid, because delete is called when x is destroyed and this happens when you exit allocater .

C++ doesn't have Garbage collector. When you create a local object, the memory is allocated on the stack and when they go out of scope, the compiler calls the destructor automatically (depends if the object is non-trivial) and frees the memory after return.

However, sometimes you need to allocate the memory dynamically. In this case, we can use new and delete to allocate and delete the memory explicitly. However, from C++11 smart pointers are introduced which are just a wrapper around the raw pointer. This helps in managing the life time of the objects.

The problem with raw pointers is that programmer has to explicitly destroy the object when it is no longer useful.

However, this is automatically take care by smart pointers.

So in your code, x is a local variable and as soon the function returns. The x is destroyed but not the memory it points to.

Many thanks for your very informative responses and feedback, really helped clarify things, using pointers feels like a game of pass the parcel!

so as long as the allocated memory has some pointer pointing to it - it can be deleted without a leak, be it returned from a function or in the main

Many Thanks

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