How to cause stack overflow and heap overflow in python

I am trying to understand how python controls the stack and heap. Therefore, I wanted to do poor programming and cause stack overflow and heap overflow. I don’t understand why the lines, for example, go on the stack, and the rest on the heap. Is this just a designer's agreement? Are the examples correct? From what I read, everything in python is generated on the heap, since its object oriented, isn't it?

EDITED . I suppose the stack in languages ​​like C has a fixed length, but in python even the stack is dynamically allocated, as his answer said in response. This is why I also get full memory if I try both a large line (on the stack) and a list (on the heap). If I am wrong, correct me. Thanks

From http://docs.python.org/c-api/memory.html

Python memory management includes a private heap containing all Python Objects and data structures. This private heap is managed by the Python internal memory manager. The Python memory manager has various components that have dynamic storage management aspects, such as sharing, segmentation, preallocation or caching.

At the lowest level, the raw memory allocator provides enough space in the private heap to store all Python-related data while interacting with the operating system's memory manager. On top of the raw memory allocator, several object-oriented allocators work in the same heap and implement different memory management policies adapted to the characteristics of each type of object.

Here are some examples. You can copy them to the official Python visualizer , but with lower values, because it does not start ...

For:

import time
word = "test "
x = word*1000000000
time.sleep(10)
print ("this message wont appear if Qaru has occurred!") 

I get

x = word*1000000000
MemoryError

If I delete one zero, it will start. I get maximum memory usage when I use it. x = word*500000000 So I can’t do a stack overflow because even the stack is dynamically allocated?

To heap overflow:

i = 10000
test_list = [0]
while i > 0 :
    test_list [:0] = test_list #insert a copy of itself at the beginning
    i -= 1

, , , . , , ? O/S? python? " " ""? , . !

EDITED
, : "" , sys.setrecursionlimit(N) N , , . - , Python .

+5
2

python, , . python, , .

>>> def foo():
...     return foo()
... 

>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  .......
  File "<stdin>", line 2, in foo
RuntimeError: maximum recursion depth exceeded
>>> 

, . , , , Python MemoryError, . " " . , , . , Python , .

, , python , . , , , .

python :

+7

, , :

, , python ( ) ( malloc). , , . , , , , .

, /- , , , , . Python alloca, ?

Cf. CPython - , ?

, ++, Fortran .., .

+5

All Articles