Reset object memory

In the game in which I am mod, they recently made some changes that violated a particular object. After talking with someone who understood to fix it, they only the information they gave me, this is what they “fixed” and will no longer share.

Basically, I'm trying to remember how to dump the contents of the memory of a class object at runtime. I vaguely remember how I did something like this before, but it was a very long time. Any help remembered on how to do this would be greatly appreciated.

+6
c ++
source share
2 answers
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"); } 
+6
source share

Well, you can reinterpret_cast your instance of the object as a char array and display it.

 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.

In any case, working with the internal elements of some implementation is usually not a good idea .

+2
source share

All Articles