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.

  • $\begingroup$ I think the most generic answer to your question would be that the compiler has arranged the positions of all the other variables in the memory as well, so whatever algorithm it's following will make sure that the next place chosen will be free. $\endgroup$
    – enobayram
    Sep 9, 2016 at 7:08
  • $\begingroup$ @enobayram: I guess that the question is more about RAM locations not being used by other processes. $\endgroup$ Sep 9, 2016 at 9:17
  • $\begingroup$ When your code is compiled, there are no variables left, each variable is replaced with the reference to a location in the stack. $\endgroup$
    – Ankur
    Sep 9, 2016 at 9:27
  • $\begingroup$ en.wikipedia.org/wiki/Memory_management_(operating_systems) $\endgroup$
    – HEKTO
    Sep 10, 2016 at 4:50

1 Answer 1


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.

  • $\begingroup$ By system you mean Memory management module of kernel. $\endgroup$
    – Ankur
    Sep 9, 2016 at 9:24
  • $\begingroup$ @Ankur, yep a component of the Operating System. $\endgroup$ Sep 9, 2016 at 9:27
  • $\begingroup$ So it's the Operating system then.But still, it sounds like a mystery that which algorithm the OS follows to know which location in RAM is empty and not a part of a page allocated to a process so that i can start at this location and end at some other location in RAM to create a new page $\endgroup$ Sep 10, 2016 at 5:03
  • $\begingroup$ @WithASpiRIT; obviously the system maintains a list of free pages and pages in use. $\endgroup$ Sep 10, 2016 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.