c - 如何读取c中的原始内存

标签 c memory memory-management

我想定义一个任意大小的缓冲区,它可以在内存中分配一些空间。我想读取分配给该缓冲区的内存内的现有数据。

我尝试过以下代码,但每次我都只是看到一些特殊字符。

如果我通过 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/

相关文章:

c - 如何自动调用C源代码中的所有函数

c - 在 C 中执行 While 循环

python - nbytes 和 getsizeof 返回不同的值

ios - arm6 设备显示更高的内存使用率

c - 变量混淆

java - 为什么我们不应该在用户消息中提供用户提供的数据?可能存在任何威胁/攻击吗?

java - JVM 页面错误应该为零,对吗?多高合适?

objective-c - Release、Dealloc 和 Self 引用

windows - 32 位应用程序在 64 位 Windows 中的行为(内存)

c - 异常: Access violation writing location when using strcpy_s