Wednesday, November 25, 2009

stack vs heap allocation

A very frequent and obvious question to mind...
what are different segments and from where storage is taken to assign space to variables...

Code segment --- compiled code, static link files etc..
Data segment -- stack and heap
stack --> function calls and variables with in function... main is also a function
heap --> static and global variables

Now big question is:
How does memory leak happens...
Suppose i define some variables in a function,
variables allocated memory in stack,
and when function call returns then all that memory is de-allocated..
By de-allocation means top pointer of stack returns to point to callee function.

Now if i do not free that variable space and set the content of memory location to null then that might be disaster because it'll result into memory leak but i dont understand why did memory leak happens... Which variable is holding that memory.. Where the pointer variable allocated memory from

Will freshen up my concept and then updated this stuff...
till then u read this...
http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html

1 comment:

  1. If memory is allocated vis malloc then usually it is allocated from Heap area otherwise it is from allocated from compile time memory.

    http://en.wikipedia.org/wiki/Dynamic_memory_allocation

    ReplyDelete