Let me summarize this issue myself. but please credit to @ecatmur and others The resize() function can be used to resize the memory buffer of an existing ctypes object. The function takes the object as first argument, and the requested size in bytes as the second argument. However, the resized object still has limited accessibility to the memory buffer based on its original size. to solve the problem. 3 different functions are defined:
def customresize1(array, new_size):
resize(array, sizeof(array._type_)*new_size)
return (array._type_*new_size).from_address(addressof(array))
def customresize2(array, new_size):
return (array._type_*new_size).from_address(addressof(array))
def customresize3(array, new_size):
base = getattr(array, 'base', array)
resize(base, sizeof(array._type_)*new_size)
new_array = (array._type_*new_size).from_address(addressof(base))
new_array.base = base
all functions return an object that shares the memory of the original owner, which does not own the memory and can not be resized (e.g., gives error in
customresize1)
customresize2does return a resized array, but keey in mind that from_address does not allocate memory for resizing..
customresize3keeps a record of the base object that owns the memory, but the returned object is not the owner of memoryAs python is dynamically allocating its memory and garbage collecting, so, if you want to resize something, just redo the size will work. eg.:
list = (c_int * NEW_SIZE)()
or you may want to keep the original values then:
list = (c_int * NEW_SIZE)(*list)
Tuesday, July 24, 2012
python ctype resize issues
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment