Are uintptr_t and size_t available?

Possible duplicate:
size_t vs. intptr_t

Some of my codes deal with pointers and accept uintptr_t as input, since it should work with pointers.

Now I need to do the same with integers, so I want to reuse this code.

Is size_t the same as uintptr_t ? Can I change the implementation and use the same code for both pointers and integers by simply replacing uintptr_t with size_t ?

+7
source share
3 answers

size_t must be large enough to contain the size of the largest possible object. uintptr_t must be large enough to contain any pointer. Given this, it is more or less guaranteed that sizeof(uintptr_t) >= sizeof(size_t) (since all bytes in the maximum possible object must be addressable), but no more. On machines with linear addressing, they are likely to be the same size. On segmented architectures, on the other hand, this is usually uintptr_t larger than size_t , since the object must be in the same segment, but the pointer must be able to address all the memory.

+16
source

It depends on the implementation (including processor, ABI, compiler, standard libraries). You have no guarantee that size_t matches uintptr_t ; but it can happen (on 32 bits of Linux x86 or ARM, both are 32 bits unsigned).

And the purpose of size_t should be the size (in particular, the allocated memory fragments), while the purpose of uintptr_t is an unsigned integer with the same bit size as the pointers.

+2
source

Different compilers have different results. If you want them to be the same, you have to make sure that your compiler is configured for 32-bit Linux x86 or ARM, and then everything will be fine.

-1
source

All Articles