When a program (Perl in this case) is executed it uses a memory section called stack. Every function call in the program adds ("pushes") a "brick of context data" on the stack, which basically holds parameters and variables local to the particular function. The brick is removed ("popped") when the function exists. With this mechanism, when you call a function recursively you get the same code/function working on different local variables, and all works without messing things up(1).
When you dynamically allocate memory (via malloc(), for example) this is taken from another bunch of memory: the heap. This is some kind of general memory repository, which is managed by the OS in a way that you normally don't have to bother with (unluckly :). When you need some memory you ask for it (malloc()); when you're done, you release it (free()) throwing it back into the heap.
The stack is generally more limited (by the OS, I suppose) than the heap; the latter can virtually suck all your memory. So, if you're using very loooong Perl lists, which get allocated in the stack, you could end up filling it and getting a "stack overflow" error, which 99% will terminate your process. On the other side, if you use very loooong arrays, you can usually ask for more space, but if you ask the OS more than it's willing to give you I guess that that will terminate your Perl process as well. If you were programming in C, you could catch the latter and try to exit gracefully, but this is another story.
(1) Note that this stack approach isn't the only that lets you manage recursion, but it's maybe the most intuitive.
Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')
Don't fool yourself.In reply to Re: Diff between heap and stack
by polettix
in thread Diff between heap and stack
by kprasanna_79
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |