3
$\begingroup$

When we declare a variable there will be a random part of memory will be allocated in RAM. Which component will allocate the memory? Is the processor or any other specific hardware doing the allocation?

$\endgroup$
1
$\begingroup$

When you compile your program, the compiler sets up (relative) positions of memory areas for variables in each piece. The linker takes several separately compiled pieces and libraries, and creates an executable file, which lays out places for each variable. The executable file is loaded into the memory by a loader, which (under the control of the operating system) loads the program at a address in memory, asigning fixed memory positions to the layout given before.

Modern programming languages also allow you to define functions/procedures/blocks with local variables, which get asigned memory addresses at runtime. When the procedure/function is called or the block entered, memory is asigned to the variable; on exit the space is reclaimed.

But then again, modern operating systems use virtual memory, so the variables will be placed at fixed virtual addresses only... the operating system shuffles the memory areas around at will.

And just to muddy the waters a bit more, while analyzing your program the compiler might decide to have the value residing in different places (in memory or in a register) during the program, have several variables share the same space when they aren't used at the same time; or sometimes even get rid of the variable completely.

$\endgroup$
2
$\begingroup$

It is a combination of the standard C library (usually) and the operating system. No hardware is involved. Check out this answer on stackoverflow for more details.

$\endgroup$
  • $\begingroup$ it was kind of brief (some system calls names also there),can you be specific and simple ) $\endgroup$ – Sivashanmugam Kannan Feb 6 '14 at 14:40
  • 1
    $\begingroup$ No, I can't be specific. You also have to do some of the work. The link I give has all the details. $\endgroup$ – Yuval Filmus Feb 6 '14 at 15:11

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.