简体   繁体   English

在内存中的特定点分配结构?

[英]Malloc a struct at a specific point in memory?

I'm trying to create a struct at a specific location in memory:我正在尝试在内存中的特定位置创建一个结构:

struct inRAM *ptr = (struct inRAM*)malloc(sizeof(struct inRAM));

But this line only allocates the memory at a place in RAM that is not retainable.但是这一行只在 RAM 中不可保留的地方分配内存。 I need to malloc beginning at a specific memory address for it to work properly, but how?我需要从特定的内存地址开始 malloc 才能正常工作,但是如何呢?

For embedded systems where you need to access specific memory addresses for I/O, you normally write directly to the address.对于需要为 I/O 访问特定内存地址的嵌入式系统,通常直接写入地址。

You don't need to malloc here, that's used to manage blocks of memory or structures where you don't care where it will be located.您不需要在这里 malloc,它用于管理您不关心它位于何处的内存块或结构块。

eg to write to address c00010例如写入地址 c00010

ptr = c00010;
ptr->field = value;

Also, when using structures overlayed onto memory be careful about padding and structure alignment.此外,当使用覆盖在内存上的结构时,请注意填充和结构对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM