gpt4 book ai didi

C 访问另一个程序内存?

转载 作者:行者123 更新时间:2023-11-30 20:50:13 24 4
gpt4 key购买 nike

我编写这段代码是为了可以看到变量 foo 的地址。

#include <stdio.h>
#include <stdlib.h>

int main(){

char* foo=(char*) malloc(1);
*foo='s';
printf(" foo addr : %p\n\n" ,&foo);
int pause;

scanf("%d",&pause);
return 0;

}

然后暂停并在此处使用 foo 的地址:

#include <stdio.h>
int main(){
char * ptr=(char *)0x7ffebbd57fc8; //this was the output from the first code
printf("\n\n\n\n%c\n\n\n",*ptr);
}

但我不断遇到段错误。为什么这段代码不起作用?

最佳答案

这不是一个 C 问题,而是运行时支持的问题。在大多数操作系统上,程序都在虚拟环境中运行,尤其是它们的内存空间。在这种情况下,内存是虚拟内存,这意味着当程序访问给定地址x时,真实(物理)内存将计算为f(x)f是操作系统实现的一个函数,用于确保给定进程(代表操作系统中代码运行的对象)拥有自己的保留内存,与专用于其他进程的内存分开。这称为虚拟内存

关于C 访问另一个程序内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48354314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com