0

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.

Polymorphism
  • 375
  • 1
  • 3
  • 11
  • 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? – Eric Postpischil Apr 29 '19 at 14:12
  • @EricPostpischil The embedded tag explains why. It is not a PC. – Lundin Apr 29 '19 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 – Julien Apr 29 '19 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. – Eric Postpischil Apr 29 '19 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. – Lundin Apr 29 '19 at 14:43
  • @Lundin: So? Hardware-related does not mean there is one specific way the assignment of an object to a specific address ought to be done. It does not tell us which of the possibilities I listed is suitable to the specific situation. – Eric Postpischil Apr 29 '19 at 14:48

2 Answers2

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));
Serge
  • 11,616
  • 3
  • 18
  • 28
  • 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. – Lundin Apr 29 '19 at 14:25
  • Thanks for sharing @Serge I will try and share the result. – Polymorphism Apr 29 '19 at 14:28
  • @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. – Serge Apr 29 '19 at 14:44
  • 1
    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. – Lundin Apr 29 '19 at 14:48
  • i do not think that OOP really meant it :-) – Serge Apr 29 '19 at 15:39
  • I try it but I got segmentation fault (core dumped) error I really do not know what's wrong with it ` int *array = (int *)0x2000008; int inputs[10] = {25, 94, 18, 64, 37, 17, 26, 83, 42, 55}; memcpy(array, inputs, sizeof(inputs)); double outputs[10];` that's my code. @Serge Sorry I could not formatting code properly – Polymorphism Apr 29 '19 at 19:14
  • how did you reserve the memory at 0x2000008 for your program? – Serge Apr 29 '19 at 20:06
0

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;
Ghaith
  • 1
  • 1