Python: Access/Save memory block just given by pointer without copying
Question: 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](http://stackoverflow.com/questions/10730841/fill-python-ctypes-
pointer) (thanks to Brian Larsen for the hint)


Answer: 
    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')
    

