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开发者_如何学Go 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.
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).
精彩评论