0

I am currently doing some development on an embedded system. I need to create an array of integers using C. But this array must be put in a certain memory address, for example, 0x12345678. All the integers in the array should be stored in a chunk together at 0x12345678.

How can I do that?

Regards, Peter

  • There is no safe, portable way to do this in standard C. But since you are working in an embedded system, there may be a way to do this for your specific application. Please provide more details on your architecture and compiler. Also, see this question for more details: http://stackoverflow.com/questions/13558995/can-address-be-assigned-to-a-variable-in-c – embedded.kyle Mar 21 '15 at 18:23
  • Isn't this the "tail wagging the dog"? One way you might do it is to put the array in its own segment. Another might be to put the (const) array into EPROM or FLASH memory which you address at the location you want. – Weather Vane Mar 21 '15 at 18:58

1 Answers1

1

It is possible to simply declare a pointer assigned with the address, then index that thus:

static int* const arr = 0x12345678 ;

However, this is often less than satisfactory because the size of the array is not defined, and if the location is in normal RAM, nothing stops the linker from instantiating other objects there. If the location refers to I/O space or memory not in the link map, this may suffice.

The safe "linker aware" method is tool-chain specific; GCC and ARM RealView for example have __attribute__ extensions for such purposes.

Clifford
  • 88,407
  • 13
  • 85
  • 165