Comment by AlienRobot
Comment by AlienRobot 6 months ago
>The implementation cleverness: use a special, impossible value of a pointer (-1) to indicate a function without arguments.
From what I know about C this code probably breaks on platforms that nobody uses.
Thanks for Sumatra, by the way :D Very useful software!
> From what I know about C this code probably breaks on platforms that nobody uses.
Your intuition is correct. Even if never dereferenced, casting a compile-time constant other than 0 to a pointer type in that way is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. [0] If I understand correctly, saving a trap representation to a variable causes undefined behaviour, as doing so presumably constitutes reading the trap value [1] (I had a tough time finding a definitive answer on that point).
I think it would be compliant to use the intptr_t type (assuming it exists on that platform) and give special treatment when it holds -1, i.e. never cast back to the pointer type if it holds -1. (Alternatively, use uintptr_t and give special treatment if it holds UINTPTR_MAX.) This would still rely on the assumption that the special value will never collide with the actual pointer values you need to work with, of course.
[0] https://wiki.sei.cmu.edu/confluence/display/c/INT36-C.+Conve...
[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2091.htm