8

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?

Community
  • 1
  • 1
Kshitij Banerjee
  • 1,678
  • 1
  • 19
  • 35

3 Answers3

19

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

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.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-2

Different compilers have different results. If you want them to be the same, you must make sure that your compiler is set to 32 bit Linux x86 or ARM, and then it will right.

cmwt
  • 351
  • 2
  • 15
funnyfan
  • 25
  • 1