Here’s a small utility function (in C) that I wrote, to dump the memory’s contents at a specified address. It’s nothing big but can be useful when debugging code.
/** * Dumps the memory at \a pData to a specified FILE* \a fpOut. * \param fpOut The FILE* where the data will be dumped. * \param szData A pointer to the beginning of the data to dump. * \param ulBytesPerLine The number of characters per line of dumped data. * \param ulLines The number of lines of data to dump. */ void dumpMemory( FILE* fpOut , const void* pData , unsigned ulBytesPerLine , unsigned int ulLines ) { unsigned int ulI, ulJ; unsigned char byC; const unsigned char* szData = (const unsigned char*)pData; // dump the data for(ulI = 0; ulI < ulLines; ulI++) { // dump the current offset fprintf(fpOut, "0x%08X ", ulI * ulBytesPerLine); // dump the current line's hex characters for(ulJ = 0; ulJ < ulBytesPerLine; ulJ++) { byC = szData[(ulI * ulBytesPerLine) + ulJ]; fprintf(fpOut, "%02X ", byC); } fputs(" ", fpOut); // dump the characters as text for(ulJ = 0; ulJ < ulBytesPerLine; ulJ++) { byC = szData[(ulI * ulBytesPerLine) + ulJ]; // display only printable ASCII characters, // and replace others with '.' if((byC >= 0x20) && (byC <= 0x7E)) { fputc(byC, fpOut); } else { fputc('.', fpOut); } } // go to the next line fputc('\n', fpOut); } // make sure all the data's been written to the FILE fflush(fpOut); }
Here is a (very) small application, demonstrating this function’s use:
#include <stdio.h> int main(int argc, char* argv[]) { char szBuf[64] = "Hello, world!\n"; dumpMemory(stdout, szBuf, 16, 4); return 0; }
This application produces the following output:
0x00000000 48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21 0A 00 00 Hello, world!... 0x00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0x00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0x00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
Hopefully, this will help others when debugging… Let me know if you find it useful!