I think it would be best to stick as closely to the language of the C
standard as possible.
I.e., using ISO/IEC 9899:1999 (E), section 6.3.2.2, paragraph 1 as a
guide:
A pointer to <b>void</b> may be converted to or from a pointer
to any function type.
A pointer to any function type may be converted to a pointer to
<b>void</b> and back
again, the result shall compare equal to the original pointer.
This just replaces "pointer to any incomplete or object type" with
"pointer to any function type"
in paragraph 1.
> I don't think it's a good idea to introduce unusual
> restrictions on what
> the void* pointer can be converted to. Can I convert it to a "void
> const*"? What about a "void *const"? Why would it need to
> be undefined
> behaviour to convert it to a char*, even though standard C intends to
> allow converting any valid void* pointer to char*?
I think these questions are largely answered by paragraph 2 of the
section quoted above:
For any qualifier q, a pointer to a non-q-qualified type may be
converted to a pointer to
the q-qualified version of the type; the values stored in the
original and converted pointers
shall compare equal.
> Also, a few details are missing from the wording in question:
>
> * Do these conversion work with null pointers? Are they
> guaranteed to
> turn null pointers into null pointers? (If not, then one
> needs to test
> the result of dlsym() before converting it to a function
> pointer, rather
> than after.)
This is answered by paragraphs 3 and 4:
An integer constant expression with the value 0, or such an
expression cast to type
void *, is called a null pointer constant. 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.
Conversion of a null pointer to another pointer type yields a
null pointer of that type.
Any two null pointers shall compare equal.
> * Is it possible for the converted void* pointer to compare
> equal to the
> address to some object? (It can happen on hardware with separate
> address spaces for code and data -- does POSIX want to support such
> hardware?)
I think POSIX should leave these areas unspecified---what does
specifying these details gain POSIX? Thus add to the above:
If a pointer to any function type, converted to a pointer to
<b>void</b>,
is used for any purpose other than conversion back to a pointer
to a
function or comparison with a null pointer, the behavior is
undefined.
In general, it would be good idea to consult Clive D.W. Feather on this.
Konrad Schwarz
|