1

I am sorry I know that's super basic question but I have to ask. I have an array in integer type and I want to Assign it to specific memory address. How can I do that with C language? For Example ; int inputs[10] ={4,10,89}; So I want to Assign this inputs to 0x20000001.

Thank you so much.

6
  • In most cases, you should not try this; you should let the C implementation assign memory locations. Not all memory is freely available for you to use in your program—the C implementation, including the operating system and loader, has arranged to use parts of it for program code, parts of it for stack, parts of it for dynamically allocated memory, and so on. You cannot just take a portion you choose without possibly interfering with that. In certain cases, there are ways to request particular memory addresses, but they are used for special purposes. Why do you want to do this? Commented Apr 29, 2019 at 14:12
  • 1
    @EricPostpischil The embedded tag explains why. It is not a PC. Commented Apr 29, 2019 at 14:24
  • What compiler ? and why do you need specific address ? One possibility with GCC is __attribute__ ((section (".mySection"))) but it depends what you want to do exactly Commented Apr 29, 2019 at 14:28
  • @Lundin: The embedded tag gives a clue. It does not answer. Whether should assign a pointer with hard-coded constant, with an address provided in a vendor-supplied header, with an assignment in the linker, with an address returned by a kernel routine, or other means depends on circumstances. Commented Apr 29, 2019 at 14:40
  • @EricPostpischil Embedded = hardware-related. I'm coding an eeprom driver as we speak, and accessing absolute addresses of physical memory directly. Every day embedded systems programming. Commented Apr 29, 2019 at 14:43

2 Answers 2

1

You are likely to get segfault due to memory access violation if you are accessing restricted area of the memory, but here's a quick code to test it out if you know what address you are writing to:

int address = 0x20000001;
int *ptr;
ptr =  (int*) address;
*ptr = inputs;
Sign up to request clarification or add additional context in comments.

Comments

1

Unless you are managing your memory allocation regions, say with linker scripts or other means, you should not do it. Compiler takes care of all memory allocation for you and does not provide any means for pre-defined memory addresses.

However, if you know what you are doing, you can use a pointer to handle it:

int *array = (int*)0x2000000; 

now you can initialize it element by element, or by memcpy.

memcpy(array, inputs, sizeof(inputs));

7 Comments

Using a pointer like that which isn't volatile qualified is almost certainly a bug. Using signed int instead of uintxx_t is almost certainly a bug.
Thanks for sharing @Serge I will try and share the result.
@Lundin volatile is needed if there is a chance of asynchronous update of this memory region from a thread or from a peripheral source. It is not needed here.
Which are about the only reasons to do this: memory-mapped registers or memory handling of some sort. If it was true ROM, then you should read a const-qualified variable instead of an absolute address.
i do not think that OOP really meant it :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.