读取和写入存储位置 [英] Read and write to a memory location

查看:71
本文介绍了读取和写入存储位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google 中做了很多研究之后,我发现下面的程序

After doing lot of research in Google, I found below program

#include <stdio.h>

int main()
{
    int val;
    char *a = (char*) 0x1000;
    *a = 20;
    val = *a;
    printf("%d", val);
}

但是它会抛出运行时间错误,在 * a = 20

But it is throwing a run time error, at *a = 20.

然后我该如何读写特定的内存

Then how can I write and read a specific memory location?

请帮助我

推荐答案

这会抛出<应当正确使用href = http://en.wikipedia.org/wiki/Segmentation_fault rel = noreferrer>段违规(SEGFAULT),因为您不知道该地址中的内容。最有可能的是内核空间,并且托管环境不希望您随意写入另一个应用程序的内存。您只应该向内存中写入您知道程序可以访问的内存,否则在运行时将发生莫名其妙的崩溃。

This is throwing a segment violation (SEGFAULT), as it should, as you don't know what is put in that address. Most likely, that is kernel space, and the hosting environment doesn't want you willy-nilly writing to another application's memory. You should only ever write to memory that you KNOW your program has access to, or you will have inexplicable crashes at runtime.

这篇关于读取和写入存储位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆