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.

In reply to Re: Diff between heap and stack by polettix
in thread Diff between heap and stack by kprasanna_79

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.