[Numpy-discussion] PyArray_DescrConverter - alignment / trailingunused bytes

Albert Strasheim fullung at gmail.com
Fri Sep 15 14:13:29 EDT 2006


Hello all

> -----Original Message-----
> From: numpy-discussion-bounces at lists.sourceforge.net [mailto:numpy-
> discussion-bounces at lists.sourceforge.net] On Behalf Of Martin Wiechert
> Sent: 15 September 2006 19:53
> To: Discussion of Numerical Python
> Subject: Re: [Numpy-discussion]PyArray_DescrConverter - alignment /
> trailingunused bytes
> 
> On Friday 15 September 2006 19:14, Travis Oliphant wrote:
> > Martin Wiechert wrote:
> > > Thanks Albert! Do you also know the corresponding C-API function? It
> > > cannot be PyArray_DescrConverter (PyObject *, PyArray_Descr **), whose
> > > signature has no "align", right?
> >
> > The DescrConverter function is meant for "O&"-style conversions.  It
> > can't accept an align function.  We could possibly add something to the
> > converter to allow specification of alignment through the object to be
> > converted.
> >

> <snip>

> One final question. To me the repr of a dtype with gaps is a little bit
> puzzling:
> 
> >>> dtype ({'names': ['a', 'b', 'c'], 'formats': ['<a4', '<f8', '<f4'],
> 'offsets': [0, 16, 24]})
> dtype([('a', '|S4'), ('', '|V12'), ('b', '<f8'), ('', '|V12'), ('c',
> '<f4')])
> 
> There should be no gap between "b" and "c" but still the repr has ('',
> '|V12')
> between them. Am I missing something?

For performance reasons, compilers will typically align integers (and
probably floats) on 4-byte boundaries and apparently, doubles on 16-byte
boundaries.

Because compilers align like this, so does NumPy. This allows you to:

1. Take any kind of C struct definition
2. Convert it to a dtype
3. Create a NumPy array with this dtype
4. Pass the array's data pointer to C code
5. Cast the data pointer to a pointer to your C struct
6. Operate on the pointer to struct as if it were allocated in C

Cheers,

Albert





More information about the NumPy-Discussion mailing list