Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a non-`void` pointer to `uintptr_t` and vice-versa

There are two related C standard rules:

C99 standard, 6.3.2.3:

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

And 7.20.1.4:

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

It means, that the following code is compliant:

int *p = NULL;
void *q = (void*)p;
uintptr_t s = (uintptr_t)q;

But does it really need the two-step cast? Will the compiler perform an implicit intermediate cast if doing something like:

int *p = NULL;
uintptr_t s = (uintptr_t)p;

(Well, it probably will on most compilers, but my question is about standard compliance)

like image 434
Eugene Sh. Avatar asked Dec 15 '15 14:12

Eugene Sh.


People also ask

Can void pointer be dereferenced?

Some Interesting Facts: 1) void pointers cannot be dereferenced. For example the following program doesn't compile.

Can you typecast pointer from void to int?

You're return ing the value of int sum by setting a void * address to it. In this case, the address is not valid. But, if you keep that in mind and get the value of sum by casting a void * to int it will work.

What is a void pointer in C++ with example?

However, the pointer is of int type. In such situations, we can use the pointer to void (void pointers) in C++. For example, // void pointer void *ptr; double d = 9.0; // valid code ptr = &d; The void pointer is a generic pointer that is used when we don't know the data type of the variable that the pointer points to.

How can I compare an unsigned integer to a void pointer?

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 int *p = NULL; void *q = (void*)p; uintptr_t s = (uintptr_t)q;

Why is the first argument to a function pointer a void?

A pointer to a structure object, suitably converted, points to its initial member ( ... ), and vice versa. The first argument is a type void* because AFAIK it is not possible to define a type of function that takes a type derived from itself. Saying simply, there is no equivalent of struct S { struct S *s; } for function types.

How do you write uintptr_t s?

Writing uintptr_t s = (uintptr_t) (void*)p; signals to a reader of your code that you know what you're doing. Honestly, if I saw that code, I would question whether the author knew what they were doing. Partly because I wouldn't have a full recollection of the relevant passages from the standard at hand.

Can null pointers be cast to 0ul?

Nevertheless, when casting a NULL pointer to a sufficiently capable integer, just think of it as a magic number, that happens to be 0UL. All pointers should be treated as opaque types, or in other words - their contents should not be your concern. Then, can NULL be checked whether it is equal to 0 or not?


2 Answers

I wouldn't risk it. The standard makes it abundantly clear what is allowed and what is not allowed.

Writing uintptr_t s = (uintptr_t)(void*)p; signals to a reader of your code that you know what you're doing.

like image 197
Bathsheba Avatar answered Sep 23 '22 18:09

Bathsheba


Any quality general-purpose implementation will process conversions between uintptr_t and a non-void type as if they'd been converted through void*. The Standard treats behavior in this situation, and many others involving pointers, as a quality-of-implementation issue, and expects that people who are seeking to write quality implementations will be able to recognize situations where there's one obvious, sensible, and useful way for a program to behave without the Standard having to explicitly enumerate them all. They also recognize that it's possible to produce an implementation which is conforming but of such poor quality as to be useless.

Although it would be possible to have an implementation where conversions between uintptr_t and any non-void pointer type behave in any arbitrary fashion of the implementer's choosing, anyone who produces an implementation where such conversions don't work in the typical fashion, and who fails to document a good reason for such difference, should be recognized as a vandal who is trying to undermine the language with their poor quality implementation. Programmers should feel no obligation to appease such behavior; unless or until it is seen for what is is, it will get worse and worse until the language becomes totally useless.

like image 34
supercat Avatar answered Sep 20 '22 18:09

supercat