-1

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?

Kevin Kelly
  • 164
  • 1
  • 12

1 Answers1

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.

hookenz
  • 36,432
  • 45
  • 177
  • 286
  • Perfect. Thanks. You helped me realize my actual problem, which is when I try to add a node to a linked list beginning at the specific point in memory, the next node was not being incremented and so it kept overwriting the original node in RAM. Simple fix: struct inRAM *ptr = head; became struct inRAM *ptr = head++; Thanks again! – Kevin Kelly Nov 05 '15 at 01:24