c - how do compilers assign memory addresses to variables? -
i teach course students ask questions programming (!): got question:
why machine choose variables go in memory? can tell store variable?
i don't know say. here's first attempt:
the compiler (not machine) chooses store variables in process address space automatically. using c, cannot tell machine store variables.
but "automatically" anticlimactic , begs question... , i've realized don't know if it's compiler or runtime or os or assignment. maybe can answer student's question better me.
the answer question quite complex since there various approaches memory allocation depending on variable scope, size , programming environment.
stack allocated variables
typically local variables
put on "stack". means compiler assigns offset "stack pointer" can different depending on invocation of current function. i.e. compiler assumes memory locations stack-pointer+4, stack-pointer+8, etc. accessible , usable program. upon return
-ing function memory locations not guaranteed retain these values.
this mapped assembly instructions similar following. esp
stack pointer, esp + n
refers memory location relative esp:
mov eax, dword ptr ss:[esp] mov eax, dword ptr ss:[esp + 4] mov eax, dword ptr ss:[esp + 8]
heap
then there variables heap-allocated. means there library call request memory standard library (alloc
in c or new
in c++). memory reserved until end of programs execution. alloc
, new
return pointers memory in region of memory called heap. allocating functions have make sure memory not reserved can make heap-allocation slow @ times. also, if don't want run out of memory should free
(or delete
) memory not used anymore. heap allocation quite complicated internally since standard library has keep track of used , unsused ranges in memory freed ranges of memory. therefore freeing heap-allocated variable can more time-consuming allocating it. more information see how malloc() implemented internally?
understanding difference between stack , heap quite fundamental learning how program in c , c++.
arbitrary pointers
naively 1 might assume, setting pointer arbitrary address int *a = 0x123
should possible address arbitrary locations in computer's memory. not hold true since (depending on cpu und system) programs heavily restricted when addressing memory.
getting feel memory
in guided classroom experience, might beneficial explore simple c code compiling source code assembler (gcc can example). simple function such int foo(int a, int b) { return a+b;}
should suffice (without optimizations). see int bar(int *a, int *b) { return (*a) + (*b);}
;
when invoking bar, allocate parameters once on stack, once per malloc.
conclusion
the compiler perform variable placment , alignment relative base-adresses obtained program/standard library @ runtime.
for deep understanding of memory related questions see ulrich drepper's "what every programmer should know memory" http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.91.957
apart c-ish country idenote
then there garbage collection popular among lots of scripting languages (python, perl, javascript, lisp) , device independent environments (java, c#). related heap allocation more complicated.
varieties of programming languages heap-based (stackless python) or entirely stack based (forth).
Comments
Post a Comment