Tom Pohl
Tom Pohl

Reputation: 3104

Python: Access/Save memory block just given by pointer without copying

Here is an example of what I try to do:

import ctypes
MEM_SIZE = 1024*1024*128

# allocate memory (this is Windows specific)
ptr = ctypes.cdll.msvcrt.malloc(MEM_SIZE)

# make memory accessible to Python calls
mem = ctypes.string_at(ptr, MEM_SIZE)
# BAD: string_at duplicates memory

# store it to disk
open(r'test.raw', 'wb').write(mem)

In a nutshell: I have a plain memory pointer, I know the size of the block and want to store it on disk or reuse it as a numpy array.

How can I do that without generating a copy of the memory block?


Related question: stackoverflow: Fill python ctypes pointer (thanks to Brian Larsen for the hint)

Upvotes: 0

Views: 884

Answers (1)

Janne Karila
Janne Karila

Reputation: 25207

ctypes_array = (ctypes.c_char * MEM_SIZE).from_address(ptr)
with open('test.raw', 'wb') as f:
    f.write(ctypes_array)

numpy_array = numpy.frombuffer(ctypes_array, dtype=numpy.byte)
numpy_array.tofile('test.raw')

Upvotes: 2

Related Questions