4

When I delete some object with the delete operator, and then create again with new operator, what is the guarantee that the object will be created at the same memory place?

Some example:

Object* obj = new Object(5);
delete obj;
Object* obj = new Object(2);
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Tom
  • 1,027
  • 2
  • 16
  • 35

4 Answers4

6

what is the guarantee that the object will be created at the same memory place?

There are no such guarantees whatsoever.

However, you will sometimes see the next object created in the same place in memory under certian circumstances. In particular, in a MSVC Debug build you might see this happen frequently. But you should never rely on this behavior. It will stop happening just when you think it is working perfectly, and it never guaranteed to happen.

If you do need to create an object in the same place in memory, there is a C++ mechanism for that, called "placement new." However I must warn you -- using this is a bit tricky because you have to establish a buffer first, then placement-new your object there, and then explicitly call the destructor yourself before creating the next object. When you're done, you have to destroy the buffer. Not to mention alignment concerns, which complicates matters to a whole other level.

There are many opportunities for bugs and mistakes when using placement-new, the code can be difficult to maintain, and the times when you actually need this functionality is exceedingly rare in normal programming. As long as I've been doing C++ professionally, I could count the number of times I needed placement-new on one hand.

If you don't absolutely , positively know that you need placement-new, you don't need it.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 1
    To quote the article `If your class needs to be aligned on a 4 byte boundary but you supplied a location that isn't properly aligned, you can have a serious disaster on your hands (if you don't know what "alignment" means, please don't use the placement new syntax)` That summed it up quite nicely for me. – Shark Aug 02 '13 at 14:34
  • @Shark: Yep, I forgot to mention alignment. That's something I deal with on a daily basis. I'll edit the post. – John Dibling Aug 02 '13 at 14:41
  • @JohnDibling Do I need any buffer? When doing something like this: `Object* obj = new Object(5);` `void* memory = obj;` `Object* obj = new(memory) Object(2);` it will be wrong? – Tom Aug 02 '13 at 15:05
  • 1
    @Tom: Yes, that will be wrong. You need to establish your own buffer, typically using an array of `char`, like this: `char* objBuf = new char [1024];` Are you sure you really need to go down this road? It's going to be a rough ride. – John Dibling Aug 02 '13 at 15:21
  • @JohnDibling OK I get it now. [Here](http://stackoverflow.com/a/9888756/1850223) I found a nice answer about `new` and `placement new`. I think just better to use normal `new`. Thanks for your help – Tom Aug 02 '13 at 15:55
  • No sweat. I purposefully did not show you how to use placement-new because I was quite sure you didn't really want to go there. :) – John Dibling Aug 02 '13 at 16:17
5

what is the guarantee that the object will be created at the same memory place?

None at all.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
1

You can't with new but you can if you want to manually assign it in memory, however not really practical. Here is more information: C++ Pointer: changing the contents without changing the address?

Community
  • 1
  • 1
Jose Sutilo
  • 908
  • 1
  • 5
  • 7
0

If you want to regenerate a project in teh memory space of the old object, look into memset.

Shark
  • 6,513
  • 3
  • 28
  • 50
  • That's usually not a good idea. C++ has better options for achieving that dubious goal, like explicit destructor calls and placement new expressions. – R. Martinho Fernandes Aug 02 '13 at 14:12
  • 1
    the correct answer to reacallocating an object in the same memory is placement new – Mgetz Aug 02 '13 at 14:14
  • @R.MartinhoFernandes So, you're saying that instead of using `memset` one would preferably write a specifical destructor that sort of does a similar thing, in a roundabout way? I mean, i though about destructors first, just never really used them in this context. I'm interested in debating this a bit, or just hearing a bit more if you don't mind. – Shark Aug 02 '13 at 14:14
  • when an object is destroyed you need to call the destructor to destroy the V-table and do cleanup. `memset` is just taking a bulldozer in without any regards to reality. – Mgetz Aug 02 '13 at 14:16
  • I see, @JohnDibling's suggestion of `placement new` is more along the lines of what I was trying to say. Agreed on the memset being a bulldozer though :D – Shark Aug 02 '13 at 14:16
  • @Shark But in general you cannot use placement new without destroying the existing object first (which you do with something like `obj.~type()`, which is an explicit destructor call). – R. Martinho Fernandes Aug 02 '13 at 14:17
  • `memset` is a bulldozer that simply won't work in many cases. Consider when you have an object that is not a POD. Or `virtual`s. – John Dibling Aug 02 '13 at 14:19
  • @JohnDibling I didn't think it thru deeply, I naively suggested using `memset` due to lack of knowledge on this `placement new`. As RMartinhoFernandes said, one would need to explicitly destroy the object, and specifically construct it using a overriden `void* operator new` that takes this nature into consideration. Preferably being allocated by an explicitly defined [memory pool](http://www.parashift.com/c++-faq/memory-pools.html) ;) – Shark Aug 02 '13 at 14:31