可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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)
回答3:
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.
回答4:
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).
回答5:
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.
回答6:
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()