I was recently asked in an interview "whats the need for Heap memory,as it shares the same memory portion with stack...its said don't depend too much on stack but its advisable to allocate memory from Heap. Now both share the same memory space in opposite directions so how does it matter??...if u are thinking of stack overflow there is also Heap overflow!!!" This was one question which left me :nonod: ...any idea what could be the reason???
Stack Limit is different from Heap Memory and also you store the pointer to the heap in Stack and its not same memory.
How is Stack limit different , can you please elaborate on it a little more..i thought they share the same memory space<stack grows towards lower memory address,heap grows towards Higher memory address>... and as for "also you store the pointer to the heap in Stack and its not same memory." consider this : #include "stdio.h" int i=malloc(10000);//i will be stored in Data Segment in the read-write portion, and it will point to Heap. int main() { free(i); return; } instead of doing a malloc, i can also use the following method: #include "stdio.h" int main() { int i[1000]=NULL;//i is allocated on stack, and will be freed when main returns. return ; } the Only difference i could come up with is dynamically controlling the size of memory,but still why are we not allowed to to use stack for dynamic allocation?????????????...
You need to understand the concept of Stack overflow and function calling method using stack and then you will understand what the above two programs have
I am still not getting it, may be i am missing something here or something i need to know...Can you clarify upon it, an example would be Gr8...
Your question is an example but you need to know what is stack and where is stack allocated. Try an infinite recursion and see what error you get. Stack will overflow but you will see that memory will be still left.
A stack frame has a fixed size and is deallocated when your function returns. So it's pretty useless for dynamic allocation. The size of a stack frame depends on the code at compile time so you can't dynamically allocate a stack frame anyway. Stack is where you store your local variables and the heap is for longer term memory usage. A heap allocation can survive many function calls. If you allocate heap memory using a local variable for the pointer then you have to store that pointer somewhere so that you don't lose track of the memory when the function returns and the original pointer is lost forever, which would be a memory leak.
Stack is also used to store function's parameters and return address. That's why the stack memory is considered "not-dynamic" - it is just too easy to break the whole program flow if you are doing something tricky to the stack by yourself while app is running. But, it is STILL possible to dynamically allocate the memory in the stack using inline assembler and esp processor register (it points to the stack). Consider a basic example: Code: { __int64 *p = 0; //pointer __asm sub esp,8h //"allocate" 8 bytes in the stack __asm mov p,esp //store the memory pointer so we can use it in C code // //use our `p' variable dynamically allocated in the stack // __asm add esp,8h //"deallocate" 8 bytes from the stack } "allocate" and "deallocate" terms are quoted because there are no such procedures for the stack, we just subtract and add to the stack pointer. It may be useful to wrap the assembly code here in some inline functions, like stack_alloc() and stack_free().
in the step "__asm sub esp,8h //allocate 8 bytes in the stack", is it possible to enter the value say 8 or anything else at runtime?...if not i don't think it can be called dynamic allocation in stack.
Yes, you can use any value there, or even read it from a variable or a parameter that is passed into the function. But this is a lot of extra complexity for no real gain -- DO NOT do this as you'll have some nasty bugs if it goes wrong.