我想定义一个任意大小的缓冲区,它可以在内存中分配一些空间。我想读取分配给该缓冲区的内存内的现有数据。
我尝试过以下代码,但每次我都只是看到一些特殊字符。
如果我通过 DumpIt 工具转储内存,然后通过十六进制编辑器打开它,我可以看到正常字符(如数字和 abc 等)。
我使用了以下使用两种技术的代码 first mentioned here第二个仅使用简单的数组并逐个遍历每个字符。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#include <time.h>
#include <cstdlib>
#include <stdint.h>
int main()
{
int x = 4;
char arr[400];
char * src;
src = arr;
uint8_t *memory = (uint8_t *) malloc(1000);
while(*memory != NULL) {
printf("Character %c\n", *(memory++));
memory++;
}
while(src != NULL) {
printf("%c ", *src);
x++;
if(x == 1000)
break;
}
printf("And the data stored in memory is %s\n", arr);
system("PAUSE");
return 0;
}
最佳答案
当你运行 malloc 时,你只会在该内存段中获得随机的东西。其中一些无法编码为 ASCII 可读的内容。您可以尝试使用“%X”而不是“%c”打印内存中内容的十六进制值。
虽然我不知道你为什么要做这样的事情。
关于c - 如何读取c中的原始内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12697740/