The function pointer will ONLY work if the program is loaded from the same address every time. The modern OS has a "randomization of address space", which leads to a random movement of the actual address of the code, data and stack - to avoid attacks that change the return address, because it is impossible to find out the address "return to" if it is chosen randomly.
There are settings to turn off random changes.
Obviously, it will also not work if the code is changed, which is located between the beginning of the code section in which the called function is located.
The pointer is converted to void * , which should be possible - it is obvious that the contents of the file will not work on another OS or processor architecture, but I see no particular reason why this does not work.
However, a more portable way would be to save the sequence number of the operation used, rather than a function pointer. Then do the following:
for(;;) switch(sequence) { case 1: f(); sequence++; break; case 2: g(); sequence++; break; } ... }
In case of failure save sequence (or sequence - 1 ).
The above assumes that the functions f and g throw an exception or use longjmp to exit [or that ... checks for errors].
Other than that, I donβt see the technical reason why
Mats petersson
source share