简体   繁体   中英

Changing the memory address of a variable

Can you change the memory address of a variable? Like where the variable sits in memory.

If so how could you go around doing that?

I am using the Compiler in Visual Studio 2019, if that helps.

Can you change the memory address of a variable? Like where the variable sits in memory.

No. You cannot change the memory address of a variable.

All objects stay in the memory address where they were created. And the programmer cannot choose that adderss for any variable (within the language); the language implementation will do that for you.

Outside the scope of C++ language, in case you are interested in choosing that address (rather than changing the address at runtime wihch is not possible), there may be language implementation specific ways available (I don't know if there are for MSVC).

I suppose you can always create a new variable, copy the contents, and delete the original. But if you need all existing references to still access the original information you would need to use a pointer, and update the pointer to the new location.

I assume you can always create a new variable, copying the contents and erasing the original. But when you need all the available references to keep accessing the original information, you would have to use a pointer and update the pointer to the new location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM