0

About a decade ago, when I played around with C++ in my high school, I learnt about pointers, and memory overflows. In C++, sometimes, using a pointer to expand (or contract) an array in place can cause it to run over (or under) the memory allotted, and cause weird behaviour. I am interpreting slice assignment in Python to be something similar to assignment by pointers:

a[:] = list(range(10000))  # Similar to using pointers in C++,
# because memory location remains unchanged.

So how does Python avoid overflows (or underflows)?


Of course, in C++, we initialize each array to a specified size, and pointer-based assignments violating that size are horrible programming practice.

10
  • "I am interpreting slice assignment in Python to be something similar to assignment by pointers" It isn't. More like "pointers with guard rails that will not let you overrun/underrun". Like in Java. You can only pass a legal index, not any arbitrary positive or negative index. Commented Apr 4, 2018 at 20:34
  • 1
    Python manages its memory behind the scenes to prevent memory overflows. You may get an out of memory exception but that's still well defined behavior. Commented Apr 4, 2018 at 20:34
  • 3
    It is a mistake to assume that just because you are modifying an existing list, your new storage must go into the same space in memory. Commented Apr 4, 2018 at 20:35
  • Incidentally, the things you're saying about C++ indicate that you need to stop using so many arrays and get used to vectors, which also resize automatically. Commented Apr 4, 2018 at 20:36
  • In (pure) Python you don't need to worry about memory at all. At least to a point where you run out of said memory, but that's another problem. Commented Apr 4, 2018 at 20:36

1 Answer 1

1

Python lists are resizable arrays. Most of the time, there is extra space to allow for growth (and shrinkage) without reallocation. When more space is needed, or too much is wasted, the internal space is reallocated. This can result in a MemoryError if more space is needed and not available. The details depend on the implementation, OS, and OS settings.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you link to a source or two where I can read and learn more? Thanks.
stackoverflow.com/questions/3917574/…; laurentluce.com/posts/python-list-implementation For more, search SO for [python] list implementation or web for python list implementation, which is what I did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.