C ++ - allocating memory in a heap using the "new"

If I have the following statement:

int *x = new int;

In this case, I allocate memory to the heap dynamically. In other words, I now have a reserved memory address for an int object.

After that, say that I did the following:

delete x;

This means that I freed up the memory address on the heap.

Let's say after that I did the following:

int *x = new int;

Will x point to the exact same old memory address that it was pointing to on the heap before it was deleted?

What if I did this before delete :

x = NULL;

And then did the following:

int *x = new int;

Will x indicate a memory address on the heap other than the old one?

Thanks.

+6
c ++ heap new-operator memory-address dynamic-memory-allocation
source share
6 answers

In the first case, you can get the same memory block again, but there is no certainty about it.

In the second case, since you did not free the memory, there is almost certainty that you will receive another block of memory with a different address. There are C ++ garbage collectors. If you used one of them, you can assume that the garbage collector will work between the moments when you displayed the pointer (so that you no longer have access to the previously allocated memory block) and ask for allocation again. In this case, you can get the same memory block again. Of course, without a garbage collector, you are simply leaking memory, so you cannot do this anyway.

+10
source share

What you ask is completely undefined (according to the C / C ++ standard) and will be implementation dependent.

it will be easy for you to verify this with your version of the compiler.

What you should notice is never to depend on the result of this behavior, since it can change at any time / with any os / even updating your compiler.

As for what I think might happen, you are likely to get the same address IF you have other threads in your program that also stand out at the same time. But even then you can get the same address if the particular malloc implementation of your compiler decides to use different heaps for different threads (which is better in terms of performance)

+2
source share

No, there is no such guarantee. It completely depends on the allocator used to satisfy your memory request. However, C ++ is a powerful beast that allows you to override a new operator. You can create a custom allocator (memory management) that provides memory in a specific way, which is sometimes very useful.

+1
source share

No, the pointer returned from the second new int; , may indicate anywhere a valid, valid entry address. There is no guarantee that a previously freed region will be used (in most cases this may not be the case as the heap manager may choose to free the actual memory at some later point in time).

+1
source share

When you do this before uninstalling:

 x = NULL; 

then this is a memory leak . You are losing a memory that serves nothing, and that you cannot be freed because you have lost its address. (you can remove the null pointer, it just does nothing)

Doing this for too long can lead to a system crash / slowdown, more acceptable, because all your memory is lost. (using swap on the disk is too much, etc. etc.)

Do not expect to receive the same new address after deletion, and then again a new one. Although this may occur from time to time, randomly.

+1
source share

1) After you free, if you select the same variable again, there is no guarantee that you will get the same piece of memory. Think about how the new is implemented, it can be done as you can imagine, even using custom allocators. In addition, it is possible that another thread used for new calls to free () and new () and "stole" your memory

2) Do not do this. You lose the link, and if you use the delete operator on the NULL pointer, it will do nothing. Thus, you may have a resource leak if you do not store a link to another variable. If you call a new one again, it will definitely highlight somewhere else, assuming no one has freed up the area provided by the first new ()

+1
source share

All Articles