Hi guys,
Just wondering is it possible to force compiler to place a constant at some specific address?
Hi guys,
Just wondering is it possible to force compiler to place a constant at some specific address?
For some compilers, it might be possible to make it place a variable somewhere. Most of the time, you probably should not need to do that.
Yes, it is;
This sort of thing works with some older operating systems (eg under MS-DOS, this will typically write the value 42 to the address 0x378, which often corresponds to the data port of the first parallel port).Code:int main() { unsigned int *address = (unsigned int *)0x378; *address = 42; }
It does not work with more modern operating systems (windows NT/2000/XP/Vista, Unix ....) as those systems are specifically designed to prevent user programs from accessing system resources (memory, etc) directly by such means. You will normally need to find some other way (eg use an system-supplied API designed to access particular devices, etc)
Technically, it is also undefined behaviour too ..... the C/C++ standards offer no guarantee of the result of dereferencing some arbitrary pointer (and a pointer with a value picked by the programmer falls in the category of "arbitrary").
Sorry, but I misstyped. I mean how can i force a compiler to build a value in ROM at address I want. I think that this must be done at compiling/linking stage. I need this to embedded system.
Thing is that I want to have let say at address 0 in rom 2B reserved for constant initially 0x0000. Then I want to be able to open that HEX file (by some software) and change the value. This value can be for example a calibrating value for I/O etc.
In software I will be using simple pointer to read that value, so there is only need to make this 2B in ROM reserved.
Hope this is clear.
And what if the address is already taken by another app/the OS? Usually the address would be stored as an offset from the start of the program, then when the OS loads it, it punches in the addresses (for where it resides in memory).
We're living in a day of protected memory here, although for embedded systems it could perhaps be different
Again, it depends on your compiler. C doesn't support this natively. Your compiler and target system must support it. RTM.