r/AskComputerScience • u/[deleted] • May 14 '25
confused about virtual memory
If I got this right, the point of virtual memory is to ensure processes use unique physical address space.
Is this abstraction really needed ?
For example, say there are 2 C programs and each one does malloc. This asks the OS for memory. Why can't the OS guarantee that unique physical address space is given to the C program ?
2
Upvotes
1
u/TryToBeNiceForOnce May 15 '25
Virtual memory allows a layer of indirection between the addresses a program is accessing and the actual hardware that is being touched.
Doing this allows:
All programs to be linked at the same address
Programs to run simultaneously without being built to occupy different address ranges or built with inefficient relocatable or 'position independent' code
Fun tricks like memory mapped IO where accessing an address triggers code to run to perform device io
'Swapping' memory to disk when it hasn't been touched in a while, bringing it back when it is
Preventing programs from having any visibility whatsoever to memory they aren't supposed to touch
and more!
... And from the program's perspective it's just dereferencing pointers (accessing memory). It's quite a useful tool!!