How to place a variable with a given absolute address in memory (with Visual C ++)

How can I statically tell Visual C ++ to place a global variable with a given absolute address in memory, like __attribute__((at(address)))

does
?

+3


source to share


1 answer


It can be done, but I don't believe there is a predefined way to do it, so it will take some experimentation. Although I don't see much benefit if you create a variable at runtime only at the beginning of user code execution.

So, first specify the section / segment where you need to initialize your variable using the highlight specifier of a specific MS. Then either run your application in a real-world scenario, unload it or debug it, and see where your variable appears. Watch out for moves (there are some ways to try to ensure there is no relocation, but they are not guaranteed to be followed at all times). Another way is to use code in an application like this one to find the address of the section you have defined.



If you cannot get consistent behavior for some reason, you can use this utility to manage the virtual address of your object file. All in all, other than the obstacles in the way, but in general I don't understand why you can't get it to work for your particular scenario if you're persistent enough.

+1


source







All Articles