调试时经常需要看内存中的一些数据,dump_mem能比较方打印出内存情况。
1 void dump_mem(void *data, int len) 2 { 3 unsigned char *p; 4 int i, n; 5 6 p = data; 7 8 printf(" Address Bytes Characters\n"); 9 printf(" -------- ------------------------------ ----------\n"); 10 for (n = 0; n < len; n++){ 11 printf(" %.8X ", (unsigned int)p); 12 for (i = 0; i < 10; i++) 13 printf("%.2X ", *(p + i)); 14 printf(" "); 15 for (i = 0; i < 10; i++){ 16 unsigned char ch = *(p + i); 17 if (!isprint(ch)) 18 ch = '.'; 19 printf("%c", ch); 20 } 21 printf("\n"); 22 p += 10; 23 } 24 }
打印出来的样式
1 Address Bytes Characters 2 -------- ------------------------------ ---------- 3 00750F40 01 00 00 00 40 00 00 00 02 00 ....@..... 4 00750F4A 00 00 00 00 00 00 00 00 00 00 .......... 5 00750F54 00 00 00 00 00 00 00 00 00 00 .......... 6 00750F5E 00 00 00 00 00 00 00 00 00 00 .......... 7 00750F68 00 00 00 00 00 00 00 00 00 00 .......... 8 00750F72 00 00 00 00 00 00 00 00 00 00 .......... 9 00750F7C 00 00 00 00 00 00 00 00 00 00 .......... 10 00750F86 00 00 00 00 00 00 00 00 00 00 .......... 11 00750F90 00 00 00 00 00 00 00 00 00 00 .......... 12 00750F9A 00 00 00 00 00 00 00 00 00 00 ..........