1

Why do we need intptr_t and uintptr_t for storing pointer addresses, even though pointers are positive values?

Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
Killbill
  • 37
  • 1
  • 3
    Pointers are not always positive values. Likely in the system you are using, pointers in the part of the address space designated for general use by user programs has a zero upper bit, making them non-negative. But addresses in the kernel/operating-system part of the address space may have the upper bit set, making them negative if converted to `intptr_t`. – Eric Postpischil Oct 09 '22 at 05:27
  • Are you asking for `C++` or `C`? They're different languages. The tag `stdint` looks like for c++. – Jason Oct 09 '22 at 05:29
  • Why does the language provide different integer types (either signed and unsigned )? Why does the language provide different pointers types (to either signed and unsigned integer types)? ... Funny question to have to ask... – Fe2O3 Oct 09 '22 at 05:31
  • 1
    The salient part that this question muddles is that `pointers will always be unsigned` since memory addresses start at 00000... The qualitative difference between the two types of pointers is the quality of the data that they point to. The compiler will generate different code for signed integer operations as compared to the code applicable to unsigned integer operations. Since an integer can be referenced through a pointer, it is vital that the code signify whether the data is being referenced as signed or unsigned for the compiler to emit the appropriate code. – Fe2O3 Oct 09 '22 at 05:53
  • 1
    you should almost always use `uintptr_t` except in very few cases [What is the use of intptr_t?](https://stackoverflow.com/a/66718120/995714) – phuclv Oct 09 '22 at 05:53

0 Answers0