Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What, exactly, is 'The Stack' as it pertains to .Net

Tags:

stack

.net

vb.net

Forgive me if this is a silly question but I'm afraid that I don't know what 'the stack' is.

I know what 'a stack' is and I've learned the FILO/FIFO acronyms. But when people say things like 'a value type is allocated on the stack, not the heap' - I'm afraid I don't really know what that means.

When I introduce a logic error into a recursive function - I'm unable to allocate any more memory to 'the stack' and my app crashes....but I don't really get what it is.

I tried to Google for an answer but only found info on 'a stack' and how to use one.

When I run a .Net App - does it create a single 'stack instance' to act as 'The Stack'? I've seen Stack traces that show me the execution levels of the code - most often when I encounter an unhanded exception...but all I remember being able to see is the methods and the order they were called...wouldn't the stack also have all the variables in scope for each step of the stack.

Maybe I'm just being silly - but I think I could imagine a situation with a recursive function where it would be handy to see the previous value of a variable - from 'the stack' but not have a need to pass it in.

Dunno if that makes any sense - it's awfully late. But I would really appreciate any information anyone has.

like image 432
Rob P. Avatar asked Jan 10 '10 07:01

Rob P.


People also ask

What is the stack and heap in C#?

In C# there are two places where an object can be stored -- the heap and the stack. Objects allocated on the stack are available only inside of a stack frame (execution of a method), while objects allocated on the heap can be accessed from anywhere.

What is stored on the stack?

A stack is a special area of computer's memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime. It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased.

What is the difference between the stack and the heap?

The major difference between Stack memory and heap memory is that the stack is used to store the order of method execution and local variables while the heap memory stores the objects and it uses dynamic memory allocation and deallocation.

Where is the stack located in memory?

As shown above, the stack segment is near the top of memory with high address. Every time a function is called, the machine allocates some stack memory for it. When a new local variables is declared, more stack memory is allocated for that function to store the variable.


1 Answers

That term refers to the call stack. You might learn more about the concepts in a low level programming or computer architecture/organization course, were you to take one.

Whenever a function is called, the return address (where it's being called from), as well as parameters for the function, are pushed onto "the stack" (a stack in memory, but no, not a C# Stack data structure that you'd read about on MSDN). When a function terminates, the address is popped from the stack and control resumes where it was originally (at the popped address). The bunde of information pertaining to a function call (local variables, return address, parameters, etc) is called a "stack frame".

So, when you use recursion (and it gets caught in an infinite recursive loop), you literally just fill the stack up with return addresses (and other data) until there is no space left.

Edit - You mentioned the heap also. This is where data structures are stored (dynamic memory allocation, through the new keyword in most languages these days, or malloc in C). In C/C++, for instance, data on the heap is there until it is explicitly freed. This is to be contrasted with local/automatic variables which are stored on the stack (and are therefore destroyed when their scope terminates... they are popped off the stack out of existence).

like image 85
Sapph Avatar answered Oct 25 '22 17:10

Sapph