It has to do with Perl in quite a tangential way - Perl is coded in C (at least before v. 6...).
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.
| [reply] [d/l] [select] |
The heap is the global amount of memory allocated to your program. The stack is a special part of the heap, which stores data in the "last in, first out" mode. It's used to store the data currently processed; for instance when entering a block, variables local to that block are allocated to the stack by growing it; the memory is freed from the stack when leaving the block.
The stack is much faster (you have only one available element at the time) to access. None of this is perl-specific, though, and perl inherited its stack and heap from C. | [reply] |
and perl inherited its stack and heap from C
well, really not, perl uses its own stack that is different from the C stack, that is used by the perl interpreter itself (because it's a C program!).
The stack is much faster (you have only one available element at the time) to access
this is not true either, in C allocating memory on the stack is much cheaper than in the heap but accessing it is roughly the same. In perl, as it uses its own memory pools to handle allocations, and actually any value is allocated on the heap (the stack only contains pointers) this doesn't apply and stack and heap are more or less equally fast.
| [reply] |
Generally heap and stack are datastructures and it is applied in memory management effectively .
Stack as you know is last in first out so whenever something is pushed or poped out it effects the elements added recenlty
whereas heap is a collection of datas all datas are grouped as a heap you can remove any element which is not needed from the heap and can allocate memory from heap if it has free memmory and this way you can achieve memory management(using delete and new)
| [reply] |