When we declare a variable, how does it get stored in a memory? How does the compiler know from within millions of memory locations which ones are currently not in use or are empty so that it can store the variable there? If it traverses each memory location the whole memory address space and checks on each location whether it is used by some other process or not, it will take like forever until it can allocate memory for a single variable.
In a multitasking system, every process receives its own virtual address space, i.e. the system maps the addresses from $0_h$ to $FFF\cdots F_h$ to available storage space which can reside partly in RAM memory and partly on disk (physical storage). The mapping is made for every process independently, allocating whole pages (typically 2K bytes) at a time.
The compiler is completely unaware of where the data will actually be stored and is free to use the whole address space. The system does the necessary housekeeping and ensures that there are no conflicts.
Actually, the mapping is completely dynamic and pages are moved between RAM and disk, following the needs of the processes and in a way to optimize the access speed. There is not even a relation between the size of the virtual address space and the amount of RAM and disk available.
To summarize, memory allocation is transparent to the compiler, as if the process was alone; only the system knowns which physical pages are free or allocated to a particular process.