[Numpy-discussion] Array data and struct alignment
Travis Oliphant
oliphant.travis at ieee.org
Sat Apr 29 15:11:07 CDT 2006
Travis Oliphant wrote:
> 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;
>> };
>>
>>
>
> In my previous discussion I was wrong. You cannot use the
> array_descriptor format for a data-type and the align keyword at the
> same time. You need to use a different method to specify fields.
>
> This, for example:
>
> descr = dtype({'names':['index', 'value'],
> 'formats':[intc,'f8']},align=1)
>
> On my (32-bit) system it doesn't produce any difference from align=0.
>
> -Travis
>
>
However notice the difference with
>>> dtype({'names':['index', 'value'], 'formats':[short,'f8']},align=1)
dtype([('index', '<i2'), ('', '|V2'), ('value', '<f8')])
>>> dtype({'names':['index', 'value'], 'formats':[short,'f8']},align=0)
dtype([('index', '<i2'), ('value', '<f8')])
There is padding inserted in the first-case. This corresponds to how
the compiler packs a short; double struct on my system. The default is
align=0. You need to use the dtype() constructor to change the
default. The auto-constructor used in dtype= keyword calls will not
change the alignment from align=0.
-Travis
More information about the Numpy-discussion
mailing list