0

What's the difference between Py_ssize_t and size_t?

When to use which one?

28th Apr 2021, 9:40 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
4 Answers
+ 2
Hi Faheem I assume you are trying to do some C coding in Python, correct? Well, Py_ssize_t is a signed integer defined in the implementation of CPython, when indexing Python arrays in C you can can use it, see: https://stackoverflow.com/questions/20987390/cython-why-when-is-it-preferable-to-use-py-ssize-t-for-indexing size_t is a native C type. It is a unsigned int and it has the size in bytes of the machine work. So, in 64 bits machines it will have 8 bytes size. It is also often used when indexing arrays, etc. So, the main differences are: 1. Py_ssize_t is signed while size_t is unsigned. 2. size_t is native to C, Py_ssize_t is defined externally by a library.
28th Apr 2021, 11:57 AM
Mark
+ 1
Hi Faheem Exactly, if you need an unsigned int, use size_t.
28th Apr 2021, 12:21 PM
Mark
0
Mark Thanks!
28th Apr 2021, 12:12 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
0
Mark if I want to use unsigned variable to index the array then should I use size_t instead of Py_ssize_t?
28th Apr 2021, 12:16 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar