Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are uintptr_t and intptr_t optional types in the C (and C++) standard?

Tags:

c++

c

c++11

c99

With C99 (and later standards) standard requires certain types to be available in the header <stdint.h>. For exact-width, e.g., int8_t, int16_t, etc..., they are optional and motivated in the standard why that is.

But for the uintptr_t and intptr_t type, they are also optional but I don't see a reason for them being optional instead of required.

like image 838
Bo R Avatar asked Nov 19 '18 17:11

Bo R


People also ask

What is Uintptr_t in C?

uintptr_t is an unsigned integer type that is capable of storing a data pointer (whether it can hold a function pointer is unspecified).

What is the use of Uintptr_t?

The intptr_t and uintptr_t types are extremely useful for casting pointers when you want to do address arithmetic. They should be used instead of long or unsigned long for this purpose. Use of uintptr_t for casting is usually safer than intptr_t , especially for comparisons.

What is intptr_t type?

intptr_t is a signed integer memsize-type that can safely store a pointer regardless of the platform capacity. The type intptr_t is similar to the types ptrdiff_t and INT_PTR. The size of the type depends upon the data model.


1 Answers

On some platforms pointer types have much larger size than any integral type. I believe an example of such as platform would be IBM AS/400 with virtual instruction set defining all pointers as 128-bit. A more recent example of such platform would be Elbrus. It uses 128-bit pointers which are HW descriptors rather than normal addresses.

like image 167
user7860670 Avatar answered Oct 10 '22 07:10

user7860670