[Python-Dev] Proper usage of size_t

Loewis Martin von Martin.vonLoewis at hpi.uni-potsdam.de
Wed Sep 3 19:40:24 EDT 2003


On Win64, the Windows SDK compiler gives tons of warnings 
that type conversions may involve truncation. For example,
in 2.3 stringobject.c, a subset of the warnings is

..\Objects\stringobject.c(285) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
..\Objects\stringobject.c(316) : warning C4244: 'function' : conversion from '__int64' to 'int', possible loss of data
..\Objects\stringobject.c(554) : warning C4244: 'function' : conversion from '__int64' to 'int', possible loss of data
..\Objects\stringobject.c(812) : warning C4267: 'function' : conversion from 'size_t' to 'int', possible loss of data
..\Objects\stringobject.c(2154) : warning C4244: 'function' : conversion from '__int64' to 'int', possible loss of data
..\Objects\stringobject.c(3410) : warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data
..\Objects\stringobject.c(3618) : warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data
..\Objects\stringobject.c(3737) : warning C4244: '=' : conversion from '__int64' to 'int', possible loss of data
..\Objects\stringobject.c(4072) : warning C4244: '=' : conversion from '__int64' to 'int', possible loss of data
..\Objects\stringobject.c(4076) : warning C4244: '=' : conversion from '__int64' to 'int', possible loss of data

These warnings occur for stuff like

  i = strlen(p);

where i is of type int, but strlen returns size_t, and that might 
exceed what int can store. Likewise, in

  _PyString_Resize(&string, s - PyString_AS_STRING(string));

the pointer difference is of type size_t, but PyString_Resize 
expects int.

I'd be willing to go through and replace "int" by "size_t" 
whereever I find it appropriate, for Python 2.4. I would keep
the current object layout for the moment, but change function
signatures, which would cause ABI breakage on platforms where
sizeof(size_t)!=sizeof(int).

What do you think?

Regards,
Martin



More information about the Python-Dev mailing list