Malloc a struct at a specific point in memory?

家住魔仙堡 提交于 2019-12-25 18:26:51

问题


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. I need to malloc beginning at a specific memory address for it to work properly, but how?


回答1:


For embedded systems where you need to access specific memory addresses for I/O, you normally write directly to the address.

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.

e.g. to write to address c00010

ptr = c00010;
ptr->field = value;

Also, when using structures overlayed onto memory be careful about padding and structure alignment.



来源:https://stackoverflow.com/questions/33534874/malloc-a-struct-at-a-specific-point-in-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!