How to print pointer to function?

Vincent Lefevre vincent+gcc@vinc17.org
Tue Dec 17 16:30:00 GMT 2019


On 2019-12-17 16:27:28 +0100, Manfred wrote:
> On 12/17/2019 2:22 PM, Segher Boessenkool wrote:
> > Is there *any* portable way to print function pointers?  Other than
> > accessing it as bytes :-)
> 
> n1570 section 6.3.2.3 p6 says "Any pointer type may be converted to an
> integer type. Except as previously specified, the result is
> implementation-defined. ..."
> 
> It says implementation-defined, not undefined,

IMHO, the difference matters mainly for the implementer: in short,
he must not ignore the issue. But an implementation-defined behavior
(or result) can be almost as useless as undefined behavior.

> so AFAIU it is legal, although you can't make assumptions on the
> actual value of the result.
> 
> However, 7.20.1.4 specifies intptr_t and uintptr_t for pointers to void
> only, and 6.3.2.3 p1 specifies the conversion to/from pointers to void and
> pointers to "any object type" which does not include functions, AFAIU.
> So, unless I am missing something, there seems indeed to be a broken link in
> the chain from pointer to function to (u)intptr_t.
> But then what's the purpose of 6.3.2.3 p6 ?

I would have actually preferred undefined behavior with a diagnostic.
I'm wondering why a programmer would want to convert a pointer to an
integer (except as a workaround to bizarre limitations, which would
better be solved directly by the standard).

-- 
Vincent Lefèvre <vincent@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 Gcc-help mailing list