[Cython] a problem with sizeof() return type
Carl Witty
carl.witty at gmail.com
Fri Feb 6 23:42:51 CET 2009
On Fri, Feb 6, 2009 at 1:59 PM, Robert Bradshaw
<robertwb at math.washington.edu> wrote:
> On Feb 3, 2009, at 12:01 PM, Michael Abshoff wrote:
>
>> Lisandro Dalcin wrote:
>>> Cython treats sizeof() as if it returns 'int', but IMHO it should
>>> be 'size_t'.
>>
>> Well, you might be technically correct since that is what the C
>> standard
>> asks for, but according to various literature, i.e. for example
>>
>> http://publications.gbdirect.co.uk/c_book/chapter5/
>> sizeof_and_malloc.html
>>
>> "The sizeof operator returns the size in bytes of its operand. Whether
>> the result of sizeof is unsigned int or unsigned long is
>> implementation
>> defined—which is why the declaration of malloc above ducked the
>> issue by
>> omitting any parameter information; "
>
> I think "int" is the best thing to return--it can always get coerced
> up to a size_t if needed, but the other way may prove problematic.
> Also, "int" is a more abstract (conceptually at least) type (better
> fitting with the ambiguity in the C standard), and we shouldn't have
> to worry about overflow in this case.
Certainly the result of sizeof can overflow an int. (Presumably on
64-bit Windows, it can overflow unsigned long, as well.)
Or are you saying that we don't have to worry about it because people
won't define such large types? That may be true in practice, but it
makes me uncomfortable.
Carl
More information about the Cython-dev
mailing list