【发布时间】:2012-10-03 06:02:48
【问题描述】:
我想定义一个任意大小的缓冲区,它可以在内存中分配一些空间。我想读取分配给该缓冲区的内存中的现有数据。
我尝试过以下代码,但我每次都只看到一些特殊字符。
如果我通过 DumpIt 工具转储我的内存,然后通过 HEX 编辑器打开它,我可以看到普通字符(如数字和 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;
}
【问题讨论】:
-
你为什么期待看到有意义的东西?另请记住,您很可能在具有虚拟内存的系统上运行;你不会像那样读取任何其他进程的内存。
-
您似乎很困惑,您无法读取随机“内存”,而虚拟内存则没有读取“原始”内存之类的东西。
标签: c memory memory-management