[Numpy-discussion] Array data and struct alignment

Travis Oliphant oliphant.travis at ieee.org
Sat Apr 29 14:58:01 CDT 2006


Albert Strasheim wrote:
> Hello all
>
> I'm busy wrapping a C library with NumPy. Some of the functions operate on a
> buffer containing structs that look like this:
>
> struct node {
>   int index;
>   double value;
> };
>
>   
[snip]
> However, this only works if struct node is tightly packed (#pragma pack(1)
> with the Visual C compiler).
>
> I don't know how feasible this is, but it would be useful if NumPy could be
> told to pack its data on n-byte boundaries or on "same as the compiler"
> boundaries. I realise that there can be problems when mixing code compiled
> by more than one compiler, etc., etc., but a simple unit test can check for
> this.
>   

When you create a data-type using the dtype(...) syntax there is an 
align keyword that will "align" the data according to how the compiler 
does it.  I'm not sure if it always works right so please test it out.

So, in your case you should be able to say.

descr = dtype([('index',intc),('value','f8')], align=1)

Note, I've eliminated some unnecessary verbage in your description.

Currently this is giving me an error that I will look into. 

-Travis











More information about the Numpy-discussion mailing list