Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
218 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...