简体   繁体   中英

How to determine the type of a C++ object from an address at run-time?

I would like to determine the type of an object from a memory address, such that I can apply the correct cast in the VS Watch window and view the object.

I am attempting to debug in the disassembly view, as my own code is loaded as a DLL into an existing exe for which I have no source code or pdb.

The existence of typeid and dynamic_cast give me hope that there is some way this could be achieved (perhaps in a limited fashion) without any additional coding.

However I am happy if the solution includes modifying my code, I have access to all the class definitions, although I can't necessarily change the in memory layout of some of them.

I also have an override for the allocation of the objects, so I have the chance to allocate extra memory for my own purposes here, eg 4 bytes before the object in which I could put a name tag of some kind.

Answering my own question. I solved this using a system designed for tracking allocations: http://www.almostinfinite.com/memtrack.html It is a drop in replacement for new that will tag all allocations with the type id being allocated (along with other information).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM