Path: csiph.com!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: =?utf-8?Q?=E2=80=9CChattyG?= takes a college freshman C/C++ programming =?utf-8?Q?exam=E2=80=9D?= Date: Tue, 10 Oct 2023 18:46:04 -0700 Organization: None to speak of Lines: 51 Message-ID: <87lecaszhf.fsf@nosuchdomain.example.com> References: <20231007235448.25c4986f123c88ff65d2f103@gmail.moc> <20231008125543.518cab44cdefed7e8af393c8@gmail.moc> <87cyxovj92.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: dont-email.me; posting-host="fae7e8aa6d7cdf9e2610e8a92119df27"; logging-data="1675177"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18b3wVIVjwpdr82M9yURZdF" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:38+uKjRsDAqxkeZ7sd3Nvqo2BD8= sha1:uS9MPcQLDJ7TP1+UDs3KnELSEOo= Xref: csiph.com comp.lang.c++:92554 James Kuyper writes: > On 10/8/23 18:31, Keith Thompson wrote: >> Blue-Maned_Hawk writes: >>> Anton Shepelev wrote: >>>> You are converting void to function pointer fptr. This is not supported >>>> in C. You might have taken advantage of void*, which is silently >>>> convertible to any pointer, but then again it does not work with >>>> function pointer, and C has not analog of void* for functions pointers, >>>> so an explicit conversion is seem inevitable. >>> >>> It mayn't be supported in standard C, but it's a common extension—in fact, >>> POSIX _requires_ it in order for the dlsym subroutine to work. >> >> The POSIX requirement is limited to values returned by dlsym(). >> >> https://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html > > I've heard that before, and it might be true - but I don't see words to > that effect on that page. It says: > > "Note that conversion from a void * pointer to a function pointer as in: > > fptr = (int (*)(int))dlsym(handle, "my_function"); > > is not defined by the ISO C standard. This standard requires this > conversion to work correctly on conforming implementations." > > As I read it, that requires that the conversion work in general - it > says nothing about it working only for values returned by dlsym. I'd say it's ambiguous. One could argue that "this conversion" refers to converting the result of a call to dlsym(). And if I were trying to create a POSIX-conforming implementation on a system where function pointers cannot in general be converted to void* and back again, I'd argue exactly that. (Imagine, for example, a system where function pointers are 128 bits and object pointers, including void*, are 64 bits, but the implementation can arrange for functions whose addresses might be returned by dlsym() to have all zeros in their high-order 64 bits.) I *think* the wording was changed at some point, and that an earlier version of POSIX required all function pointers to be convertible to void*, but I can't find a reference. (opengroup.org has links to older versions of POSIX from 2008, 2013, and 2016, but when I search for "dlsym" in any of them it takes me to the current version.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */