Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I came across the following code snippet on the SEI CERT C Coding Standard wiki: Compliant Solution Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change i...
#7: Post edited
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to `intptr_t` or `uintptr_t` and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a `char *` pointer to a `uintptr_t`, as in this compliant solution, is allowed on implementations that support the `uintptr_t` type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to `void` may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to `void` and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to `intptr_t` or `uintptr_t` and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a `char *` pointer to a `uintptr_t`, as in this compliant solution, is allowed on implementations that support the `uintptr_t` type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectively (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to `void` may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to `void` and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#6: Post edited
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
> Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
> 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to `intptr_t` or `uintptr_t` and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a `char *` pointer to a `uintptr_t`, as in this compliant solution, is allowed on implementations that support the `uintptr_t` type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to `void` may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to `void` and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#5: Post edited
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#4: Post edited
Casting a non-`void` pointer to `uintptr_t` directly
- Casting a non-`void` pointer to `uintptr_t`
#3: Post edited
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the intermediate cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#2: Post edited
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.
- -------------------------------------
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
- I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer):
- > **Compliant Solution**
- >
- > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.
- >
- > ```
- > #include <stdint.h>
- >
- > void f(void) {
- > char *ptr;
- > /* ... */
- > uintptr_t number = (uintptr_t)ptr;
- > /* ... */
- > }
- > ```
- The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest.
- My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1).
- So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`.
- -------------------------------------
- **References:**
- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331):
- > 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`
- [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63):
- > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
- [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64):
- > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
#1: Initial revision
Casting a non-`void` pointer to `uintptr_t` directly
I came across the following code snippet on the SEI CERT C Coding Standard [wiki](https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Converting+a+pointer+to+integer+or+integer+to+pointer): > **Compliant Solution** > > Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value. (See INT36-EX2.) The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type. > > ``` > #include <stdint.h> > > void f(void) { > char *ptr; > /* ... */ > uintptr_t number = (uintptr_t)ptr; > /* ... */ > } > ``` The first two sentences are referring to [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331) and [§6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63), respectivey (reproduced below for reference). However, I am not sure if these clauses permit us to cast a non-`void` pointer directly to `uintptr_t` as the third sentence seems to suggest. My understanding is that §7.22.1.4(1) and §6.3.2.3(1) only apply to `(uintptr_t)(void *)ptr`. Once we drop the `(void *)`, we end up with `(uintptr_t)ptr`. §7.22.1.4(1) and §6.3.2.3(1) no longer apply and we have to fall back on [§6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64), which says that the result of the cast is *implementation-defined*. There does not seem to be any guarantee that this result is the same as that of `(uintptr_t)(void *)ptr`. Nor is there any guarantee that `(char *)(uintptr_t)ptr` recovers the original pointer, since §6.3.2.3(6) does not make any roundtrip guarantees like those of §7.22.1.4(1) and §6.3.2.3(1). So I don't think `(uintptr_t)ptr` can be said to be equivalent to `(uintptr_t)(void *)ptr` and I wonder if it is really safe to omit the cast to `void *`. ------------------------------------- [N3220 §7.22.1.4(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=331): > 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` [N3220 §6.3.2.3(1)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=63): > 1. A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer. [N3220 §6.3.2.3(6)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#page=64): > 6. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.