7

If I have the following statement:

int *x = new int;

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

Say after that that I made the following:

delete x;

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

Say after that I did the following again:

int *x = new int;

Will x point to the same old memory address it pointed to at the heap before it was deleted?

What if I did this before delete:

x = NULL;

And, then did this:

int *x = new int;

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

Thanks.

trincot
  • 317,000
  • 35
  • 244
  • 286
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 1
    By just setting a pointer to `NULL`, you are not deleting it. If you then do `new int`, the old memory will still be in use (without a pointer -- it's a memory leak), and of course `new` will *always* point to a *new* address -- hence the name. – Felix Dombek Jan 29 '11 at 09:33

6 Answers6

11

In the first case, you might get the same block of memory again, but there's no certainty of it.

In the second case, since you haven't freed the memory, there's a near-certainty that you'll get a different block of memory at a different address. There are garbage collectors of C++. If you used one of these, it's conceivable that the garbage collector would run between the time you NULLed out the pointer (so you no longer had access to the previously-allocated memory block) and asking for an allocation again. In such a case, you could get the same memory block again. Of course, without a garbage collector, you're just leaking memory, so you don't way to do this in any case.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3

What you are asking is entirely undefined (as per C/C++ standard) and would depend on the implementation.

it should be easy for you to test this out with your version of the compiler.

What you should note though is to never depend on the outcome of this behavior as it can change any time / with any os / even an upgrade to your compiler.

As to what i think might happen, you would get the same address most likely, UNLESS you have other threads in your program which are also allocating during the same time. But even then you might 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 perf)

computinglife
  • 4,321
  • 1
  • 21
  • 18
2

When you do this before delete:

x = NULL;

then it is a memory leak. You lose memory that serves nothing, and that you cant free because you lost the address of it. (you can delete a null pointer, it just does nothing)

Doing this too much time can result in a system crash / slowing down more that acceptable because all your memory is wasted. (using swap on disk too much etc... etc...)

Dont expect to get the same exact new address after a delete, then a new again. Though it may happen from time to time, randomly.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
1

No, there's no such guarantee. This depends entirely on the allocator used to satisfy your memory request. However, C++ being the powerful beast that it is, let's you override the new operator. You can build a custom allocator (memory managment) which provide memory in a certain manner, which sometimes is very useful.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • Could you give an example -- how would one allocate memory without using `new` and what is a custom allocator used for? – Felix Dombek Jan 29 '11 at 09:31
  • Custom allocators can be used to provide a fixed amount of sequential memory. Performance wise and typically in games it's preferable to have a nice sequential memory access patterns. A garbage collector (automatic memory management) generally provides this by allocating this close together in time. A simple linear allocator could do the same and it can also move around things on a small heap as things get deleted to prevent holes and take into consideration other hardware constraints to future improve memory performance. – John Leidegren Jan 29 '11 at 09:41
  • For more examples you could see this page http://www.cprogramming.com/tutorial/operator_new.html I'm quite sure it's possible to override the default allocator, by which, you would override the `new int` operation. – John Leidegren Jan 29 '11 at 09:41
1

No, the pointer returned from second new int; can point anywhere within the valid writable address reange. There is no guarantee that the previously deallocated region will be used (in fact most of the times it may not as the heap manager may choose to release the actual memory at some later point of time).

Asha
  • 11,002
  • 6
  • 44
  • 66
1

1) After you free, if you allocate again using the same variable there is no guarantee you are going to get the same chunk of memory. Think about how new is implemented, it can be done any way you can imagine even using custom allocators. Also, it's possible that other thread used new between free() and new() calls and 'stole' your memory

2) Don't do it. You are losing reference and if you apply delete operator on NULL pointer it will do nothing. So you might have a resource leak if you don't store the reference on other variable. If you call new again it will definately allocate somewhere else assuming that noone deallocated the area granted by first new()

tdobek
  • 1,156
  • 1
  • 8
  • 6