Avoid using an operation that creates a temporary copy, or creates temporary objects during iteration. This should be a direct copy from the source buffer to the destination buffer. The easiest way is to use ctypes.memmove :
f = Foo() f.f1=1 f.f2=2 buf = (c_char * 100)() memmove(buf, byref(f), sizeof(f))
Or use a temporary array created with from_buffer (not a copy):
memoryview(buf)[:sizeof(f)] = (c_char * sizeof(f)).from_buffer(f)
However, the latter does not work in Python 3. It should work, but the buffer format codes used by ctypes are not supported by memoryview .
If you want the new array to be the same size or smaller, you can use the from_buffer_copy (2.6 +) array method:
buf = (c_char * sizeof(f)).from_buffer_copy(f)
source share