tags:

views:

38

answers:

2

In a game that I mod for they recently made some changes which broke a specific entity. After speaking with someone who figured out a fix for it, they only information they gave me was that they "patched it" and wouldn't share anymore.

I am basically trying to remember how to dump the memory contents of a class object at runtime. I vaguely remember doing something similar before, but it has been a very long time. Any help on remember how to go about this would be most appreciated.

+1  A: 

Well, you may reinterpret_cast your object instance as a char array and display that.

Foo foo; // Your object
 // Here comes the ugly cast
const unsigned char* a = reinterpret_cast<const unsigned char*>(&foo);

for (size_t i = 0; i < sizeof(foo); ++i)
{
  using namespace std;
  cout << hex << setw(2) << static_cast<unsigned int>(a[i]) << " ";
}

This is ugly but should work.

Anyway, dealing with the internals of some implementation is usually not a good idea.

ereOn
+2  A: 
template <class T>
void dumpobject(T const *t) {
    unsigned char const *p = reinterpret_cast<unsigned char const *>(t);
    for (size_t n = 0 ; n < sizeof(T) ; ++n)
        printf("%02d ", p[n]);
    printf("\n");
}
Didier Trosset
Thank you, this is exactly what I was looking for.
David Mathers
Please note that this only works if the object does not contain pointers to memory elsewhere.
doron
Also, if you pass a `Base*` pointer, it will not print the full `Derived` object, just the `Base` part.
MSalters