Discussion:
[Numpy-discussion] correct sizeof for ndarray
Jason Newton
2015-10-20 04:05:51 UTC
Permalink
Hi folks,

I noticed an unexpected behavior of itemsize for structures with offsets
that are larger than that of a packed structure in memory. This matters
when parsing in memory structures from C and some others (recently and
HDF5/h5py detail got me for a bit).

So what is the correct way to get "sizeof" a structure? AFAIK this is the
size of the last item + it's offset. If this doesn't exist... shouldn't it?

Thanks,
Jason
Allan Haldane
2015-10-20 13:56:02 UTC
Permalink
Post by Jason Newton
Hi folks,
I noticed an unexpected behavior of itemsize for structures with offsets
that are larger than that of a packed structure in memory. This matters
when parsing in memory structures from C and some others (recently and
HDF5/h5py detail got me for a bit).
So what is the correct way to get "sizeof" a structure? AFAIK this is
the size of the last item + it's offset. If this doesn't exist...
shouldn't it?
Thanks,
Jason
Hi Jason,

The 'itemsize' attribute of a dtype object is probably what you're
looking for. It gives the itemsize in bytes.

"last item + it's offset" is not a reliable way to get the itemsize
because "aligned" (and other) structures can have trailing padding, just
Post by Jason Newton
dtype('i4,u1', align=True).itemsize
8

The documentation on all this is a little scattered right now, but there
are hints in the array.dtypes reference page and the dtype docstring.

Cheers,
Allan

Loading...