_ptr and _srcptr types

Vincent Lefevre vincent at vinc17.net
Tue May 22 06:18:18 UTC 2018


On 2018-05-22 04:46:50 +0200, Pedro Gimeno wrote:
> The C11 standard also says:
> 
> P.47:
> 
> 1. 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.
> 
> (Definition of object, p.5 §3.14: "region of data storage in the execution environment, the contents of which can represent values")
> 
> [...]
> 
> 3. An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
> 
> 4. Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.
> 
> Doesn't that allow using (void *)0 for the suggested purpose?

No, with variadic functions, a null pointer of some type is provided
as an argument, and then the value is re-read as another pointer type
(this is actually a simplification, because depending on the calling
conventions, using incompatible types can just be meaningless). You
do not have a conversion.

For instance, something similar can be said between the 0 of type
int and the 0 of type long: in a type conversion, the value does
not change, and (int) 0 and (long) 0 compare equal. But if int is
on 4 bytes and long is on 8 bytes, they are not interchangeable in
a variadic function. Note that for pointers, this is even worse,
since even in the case the size and alignment requirements are the
same, the representation of the null pointer may still depend on
the type.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-devel mailing list