John
John

Reputation: 123

Why Virtual Memory Address is the same in different process?

https://i.sstatic.net/RGMsz.png

I know the virtual memory separates 2G(for kernel)/2G(for user) in Windows. But why the address of variable isn't stack continually?

Likes 0x22ff74,0x22ff78,0x22ff82,0x22ff86 ? Does it mean that Windows use sandbox mechanism in user process?

Upvotes: 4

Views: 2968

Answers (3)

Ajay
Ajay

Reputation: 18421

Let me explicate it another way. Process X is running on machine A, and the same program is running as process Y on machine B. Does it matter if some global variable of your program takes same memory address on both machines? They are different! The same way, if that global variable is stored at XYZ location for one instance of process, another instance of process may have the same virtual address (XYZ) for that global variable.

Upvotes: 0

zr.
zr.

Reputation: 7788

Are you confusing physical addresses and virtual addresses? It's ok for two processes to access the same virtual address, because each process see its own virtual memory space. On the other hand, all processes share the same physical memory space in the machine, so each process will have that same virtual address mapped to a different physical address (assuming there is no shared memory).

Upvotes: 3

user1157123
user1157123

Reputation:

That's exactly what virtual memory is. The operating system provides each program with its own private address space. In reality the operating system is in charge of mapping those virtual addresses back to the physical address space without the application being aware.

As you noticed this means that two applications can have different data residing at the same virtual address in the program.

Read more about virtual memory here.

Upvotes: 15

Related Questions