I copied the code in this article to implement a dynamic array in python using the ctypes module. Following the author's advice, I began to check the array's size as follows:
import sys
arr = DynamicArray()
for i in range(12):
arr.append(i)
a = len(arr)
b = sys.getsizeof(arr)
print(a, b)
However, the value for b was 56 each time, the same as the size of the empty array.
Does anyone understand what's happening? Why does the array seem to be storing more elements without taking up any more size in the memory?
from pympler import asizeofand thenasizeof.asizeof(arr)instead. Pympler'sasizeof.asizeof()gives a more complete estimate of object size.asizeof.asizeof(arr), where it just returned the same value no matter how many items were stored inarr.