gpt4 book ai didi

c++ - 如何在特定的内存位置写入数据? C++

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:07 30 4
gpt4 key购买 nike

以下是我用来在 0x10000000 处写入无符号数据的代码。程序已编译但运行失败。

void load_program(unsigned base_address){
char* IM=reinterpret_cast <char*>(base_address);
unsigned a=0;
*IM=a;
}
int main(int argc, char** argv) {
unsigned address=0x10000000;
load_program(address);
return 0;
}

最佳答案

操作系统实际上不允许您访问不是通过操作系统接口(interface)分配的内存。内存管理非常复杂(引用 https://en.wikipedia.org/wiki/Paging 作为示例)。

您的代码应该在没有操作系统的设备上运行,例如 Arduino。

无论如何,如果你想管理自己的内存,也许你可以先通过调用malloc(这是老式的 C 风格)来分配一个 block

int * pointer = 0;
int size = 50000;
pointer = (int*) malloc(size); //pointer now points to the beginning

关于c++ - 如何在特定的内存位置写入数据? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48564091/

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