Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are uintptr_t and size_t same? [duplicate]

Possible Duplicate:
size_t vs. intptr_t

Some of my code deals with pointers and takes uintptr_t as input since it has to work with pointers.

I now have to do the same thing with integers, So I want to reuse that code.

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

like image 772
Kshitij Banerjee Avatar asked Dec 28 '12 09:12

Kshitij Banerjee


2 Answers

size_t has to be big enough to contain the size of the largest possible object. uintptr_t must be big enough to contain any pointer. Given this, it is more or less guaranteed that sizeof(uintptr_t) >= sizeof(size_t) (since all of the bytes in the largest possible object must be addressable), but not more. On machines with linear addressing, they probably will be the same size. On segmented architectures, on the other hand, it is usual for uintptr_t to be bigger than size_t, since an object must be in a single segment, but a pointer must be able to address all of the memory.

like image 138
James Kanze Avatar answered Oct 01 '22 10:10

James Kanze


It depends upon the implementation (and that includes the processor, the ABI, the compiler, the standard libraries). You have no guarantee that size_t is the same as uintptr_t; but that could happen (on 32 bits Linux x86 or ARM, both are 32 bits unsigned integers).

And the intent of size_t is to be a size (notably of allocated memory chunks), while the intent of uintptr_t is to be an unsigned integer of the same bit size as pointers.

like image 43
Basile Starynkevitch Avatar answered Oct 01 '22 09:10

Basile Starynkevitch