[Numpy-discussion] frombuffer alignment for ctypes.Structure array

Roland Schulz roland@utk....
Fri Apr 10 02:12:41 CDT 2009


I would like to create a ndarray view on an array of ctypes.Structure.
(reason: see below).

The problem is that the structure size is not falling on an alignment
boundary and thus the size is bigger than its parts.

A sample showing the problem:

import numpy as N
from ctypes import *
class C(Structure):
_ctypes_to_numpy = {c_short : N.int16,c_int : N.int32}
ty = [(x,_ctypes_to_numpy[y]) for x,y in C._fields_]

The is no align or aligned option to frombuffer. What is the best way to
tell numpy to align the data as the C-struct/ctypes.Stucture array is?

I have an array which get allocated in the external C library and thus I get
a pointer on structs from that C function.
The only way I know how to create a ndarray view for pointers on data, is to
first convert the ctypes pointer to a ctypes array.
I convert the POINTER(t_X) to an ctypes array of the structre using the
trick, where t_X is the data type of the struct and X the pointer to the


ORNL/UT Center for Molecular Biophysics cmb.ornl.gov
865-241-1537, ORNL PO BOX 2008 MS6309
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090410/9b9c32df/attachment.html 

More information about the Numpy-discussion mailing list