0

I created my own memory manager by overloading the new and delete operators of my base class. The overloaded operators maintain a linked list of memory allocations and I also store the file and line number of the allocation. It works with multiple inheritance as well, since the delete operator always receives the correct memory address (i.e. the address I returned from 'new'). What is the best way for me to get the memory address of the object (i.e. the memory address of the first parent)?

I want to create methods that, at run-time, can return the allocation file and line number. With multiple inheritance the 'this' pointer might not point to the correct memory address (it is sometimes offset by 8 bytes). I understand why it is offset, but I'm looking for a way to reliable calculate the correct memory address originally returned by the new operator.

Arno Duvenhage
  • 1,910
  • 17
  • 36

2 Answers2

2

If the class hierarchy is polymorphic, you can use dynamic_cast<void*>(this) to obtain the address of the most derived object of which *this is a subobject.

See also this question.


If your class hierarchy is not polymorphic, you cannot know dynamically whether a given base object is most-derived, and if it is not, you cannot know what its most-derived object is. However, in that case you also cannot destroy the object since you don't have access to the most-derived destructor, and so you will generally not have need to releasing the memory. It is true, however, that ou cannot look up an object's allocation trace if you cannot determine its most-derived object.

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 3
    The anonymous downvoter probably downvoted out of sheer ignorance, as usual. However, I think this answer suffers from just being an *in-practice* answer. AFAIK there is no guarantee that the most derived object's address is the address returned by `operator new`. – Cheers and hth. - Alf Jul 19 '14 at 13:14
  • @Cheersandhth.-Alf: I don't think there's room for this to go wrong: When you say `new Derived`, it will use the inherited `operator new` to get exactly `sizeof(Derived)` memory, and it will construct the object at the returned address. – Kerrek SB Jul 19 '14 at 13:17
  • well that's where I'm absolutely not sure you're right. but I think it works *in practice*. – Cheers and hth. - Alf Jul 19 '14 at 13:17
0

You cannot do it. Each C++ compiler is allowed to do whatever it wants with the storage you return from operator new. It can choose any sort of layout, and then the result of the new operation is some pointer into somewhere in the memory returned from the operator. The address of the most derived object is not the same as the value returned by the operator.

Since you haven't specified that you are using some particular version of some particular compiler, no one can give you an answer that depends on knowledge of the implementation-dependent specifics.

bmargulies
  • 97,814
  • 39
  • 186
  • 310