3

Rationale for C, Revision 5.10, April-2003:

Even with an explicit cast, it is invalid to convert a function pointer to an object pointer or a pointer to void, or vice versa.

Why exactly it is invalid to convert a function pointer to a pointer to void, or vice versa?

What are the conceptual / technical obstacles to allow such conversion?

Reason: better understanding of the standard.

UPD. FYI: In POSIX dlsym returns valid object opened by dlopen() as void*. Then such void* object is usually converted to a function pointer (which is invalid per C) and called. More details: link.

TylerH
  • 20,799
  • 66
  • 75
  • 101
pmor
  • 5,392
  • 4
  • 17
  • 36
  • 3
    FWIW, it is recognized as a common extension https://port70.net/~nsz/c/c11/n1570.html#J.5.7 - Myabe it isn't strictly required in the interest of being applicable to some potato that treats data and code very differently. – StoryTeller - Unslander Monica Jan 12 '22 at 15:23
  • 1
    Technically speaking it probably doesn't make sense to convert a function pointer to an object pointer on a lot of systems. There's Harvard architecture and there's systems with MMU hardware protection against running code from data memory or r/w accessing code memory. Low-end von Neumann without MMU however, can likely merrily do whatever you tell it to, like trying to execute code in the data section. In fact it's somewhat common practice for bootloaders etc on such systems, where the OP codes are uploaded in flash memory or even RAM through variables, then executed. – Lundin Jan 12 '22 at 15:42
  • @Lundin Re: "There's Harvard architecture...": yes, exactly. The "C doesn't limit itself to only certain types of architectures". – pmor Jan 13 '22 at 17:57
  • 1
    There could be a platform, where data-pointers are 16-bit long, while code-pointers are 24-bit long but the LSB is always zero. Conversion in either direction would end in losing information. (Though many platforms (eg Win32/64, Posix, BS2000) have uniform address-space.) – Zsigmond Lőrinczy Jan 14 '22 at 09:30

0 Answers0