Home > Software engineering >  What is the appropriate arithmetic type for a pointer?
What is the appropriate arithmetic type for a pointer?

Time:10-15

In my code I have two char pointer, one to a string and the other as an error indicator for strtoumax() and strtod(). I am currently using the type size_t (aka unsigned long) to calculate the difference between them. Is there any type designed to specifically match pointer type size on every machine? Or do I have to check it myself with macros?

CodePudding user response:

For pointer difference use ptrdiff_t. If you're just trying to store a pointer as an integer, use uintptr_t (or intptr_t).

CodePudding user response:

In my code I have two char pointer, one to a string and the other as an error indicator for strtoumax() and strtod(). I am currently using the type size_t (aka unsigned long) to calculate the difference between them.

Don't do that. If you want a pointer difference then compute a pointer difference:

#include <stdint.h>

// ...

ptrdiff_t difference = p2 - p1;

And note ptrdiff_t, which is the type of the result of a pointer difference.

If you want a difference in bytes instead of in units the size of the pointed-to type (including if the pointed-to type is incomplete, such as void) then first convert to pointers to char:

ptrdiff_t difference_in_bytes = (char *) p2 - (char *) p1;

(char is the smallest addressible unit of storage, but technically, it might be larger than 8 bits on some C implementations. CHAR_BIT will help you figure that out if you're concerned about such cases.)

Do not compute a pointer difference by converting to integer and performing integer arithmetic, because although the behavior of that is defined ( /- signed integer overflow), the meaning of the result is not.

Is there any type designed to specifically match pointer type size on every machine? Or do I have to check it myself with macros?

Yes. In stdint.h there are definitions of uintptr_t and intptr_t, which can support round-trip pointer to integer to pointer conversions without data loss. But C does not define the meaning of the value resulting from converting a pointer to an integer, so these are best used as opaque types.

CodePudding user response:

To store pointers as integers you can use intptr_t and uintptr_t declared in the header <stdint.h>.

From the C Standard (7.20.1.4 Integer types capable of holding object pointers)

1 The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

intptr_t

The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

uintptr_t

These types are optional.

To store difference between two pointers you can use ptrdiff_t declared in the header <stddef.h>.

Pay attention to that you may calculate difference between two pointers if they both point to elements of the same array or one past the last element. Otherwise you will get undefined behavior.

  • Related