[Numpy-discussion] Re: int16 or int32 for C int ?
nicopernetty at yahoo.fr
Mon Jun 27 16:46:24 CDT 2005
On Mon, 27 Jun 2005 10:40:13 -0700, Scott David Daniels
<Scott.Daniels at Acm.Org> wrote :
> >>>I would like to create a numarray array from a C buffer of 'int'
> >>>but I don't quite know how to handle different 'int'
> >>>representation (16 or 32 bits).
> > ... It was to be interfaced with legacy code which include 'int'
> > declaration (which I couldn't change). So I think that I'll keep the
> > 'sizeof' trick.
> I would not treat the test as either-or, but rather as a triple.
> if (sizeof(**a) == 4) x = NA_NewArray(a, tInt16, 2, 10, 20);
> else if (sizeof(**a) == 2) x = NA_NewArray(a, tInt16, 2, 10, 20);
> else abort(); /* or raise some exception here. */
> If you compile this on a machine with 64-bit ints, it is better if it
> fails here than proceed as if working with 2-byte ints. If I am sure
> I'll never run into that case, I'd do it as above (with the abort()).
> A good C compiler can realize the test is constant and simply generate
> one arm of the conditions anyway. If I think it might possibly be
> used on a 64-bit int system, I'd go ahead and worry about what
> exception to raise (probably PyTypeError, but ...).
I was so sure that 'int' could only be 16 or 32 bits then I found this
Well thank you for your suggestion, I'll adopt it !
More information about the Numpy-discussion