A
A
Alexander Nikolaev2020-07-30 21:57:12
C++ / C#
Alexander Nikolaev, 2020-07-30 21:57:12

Is it possible to assign a variable/pointer a specific address in memory?

I welcome everyone! I'm new to C++, so this might sound like a dumb question.

For example, I want to assign the address 0046FCF0 to a variable/pointer. Is it possible to do this, and if so, how? I didn't find an answer on google.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2020-07-31
@res2001

You can assign an address to a pointer. In fact, a pointer is just an integer variable, the bit width of which is equal to the bit width of the address bus. But pointer arithmetic has rules different from ordinary arithmetic (address arithmetic).
Referring to the assigned address (dereferencing, getting the value at the specified address), if you don’t know exactly what you are doing, is not worth it, because this can lead to an access violation (segmentation fault).
Modern operating systems use virtual memory, which is why the memory used by the program must be correctly allocated using the mechanisms provided by the OS (new).
Microcontrollers do not have virtual memory, so all memory is available to the program at once. There, you can freely access any address within the physically accessible address space.

N
none7, 2020-07-31
@none7

int *p = (int*)0x0046FCF0;
Only here in the current OS usually there are no fixed addresses. This may be necessary, except when programming microcontrollers or drivers for ARM.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question