Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can cause a stack overflow quite easily in python, as in any other language, by building an infinately recursive funcion. This is easier in python as it doesn't actually have to do anything at all other than be recursive.</p> <pre><code>&gt;&gt;&gt; def foo(): ... return foo() ... &gt;&gt;&gt; foo() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; ....... File "&lt;stdin&gt;", line 2, in foo RuntimeError: maximum recursion depth exceeded &gt;&gt;&gt; </code></pre> <p>As for the heap, that is managed by a garbage collector. You can allocate lots of objects and eventually run out of heap space and Python will raise a <code>MemoryError</code>, but it's going to take a fair amount of time. You actually did that with your 'stack overflow' example in the question. You stored a reference to a string on the stack, this string took up all the free memory available to the process. As a rule of thumb, Python stores a reference to a heap structure on the stack for any value that it can't guarantee the size of. </p> <p>As for how it all works, from the fist example you can see that python has a built-in limit to the depth of the call stack that it will not exceed. The amount of memory available for heap space is defined by the OS however and will depend upon many factors. </p> <p>These are should be the appropriate parts of the python docs for infomation on the errors themselves:</p> <ul> <li><a href="http://docs.python.org/release/3.2.3/library/exceptions.html#RuntimeError">http://docs.python.org/release/3.2.3/library/exceptions.html#RuntimeError</a></li> <li><a href="http://docs.python.org/release/3.2.3/library/exceptions.html#MemoryError">http://docs.python.org/release/3.2.3/library/exceptions.html#MemoryError</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload