Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Any Idea regarding Why Heap Memory (http://www.go4expert.com/forums/idea-regarding-heap-memory-t21452/)

micsom 22Mar2010 17:29

Any Idea regarding Why Heap Memory
 
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???

shabbir 23Mar2010 11:45

Re: Any Idea regarding Why Heap Memory
 
Stack Limit is different from Heap Memory and also you store the pointer to the heap in Stack and its not same memory.

micsom 23Mar2010 12:34

Re: Any Idea regarding Why Heap 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?????????????...

shabbir 23Mar2010 12:44

Re: Any Idea regarding Why Heap Memory
 
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

micsom 23Mar2010 13:00

Re: Any Idea regarding Why Heap Memory
 
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...

shabbir 23Mar2010 13:03

Re: Any Idea regarding Why Heap Memory
 
Quote:

Originally Posted by micsom (Post 66115)
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.

xpi0t0s 24Mar2010 04:04

Re: Any Idea regarding Why Heap Memory
 
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.

micsom 24Mar2010 10:32

Re: Any Idea regarding Why Heap Memory
 
Quote:

Originally Posted by xpi0t0s (Post 66143)
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.

Thats good information...didn't think of it in this way...Thanks for the information.

davidk 26Mar2010 12:46

Re: Any Idea regarding Why Heap Memory
 
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().

micsom 29Mar2010 12:23

Re: Any Idea regarding Why Heap Memory
 
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.


All times are GMT +5.5. The time now is 09:01.