3 ms·
No harm. :) If you want a good example of where they're useful, write the interpreter loop for a simple 3-byte wordsize VM. To expand on that, make an array o
by angersock 15y ago
No harm. :)
If you want a good example of where they're useful, write the interpreter loop for a simple 3-byte wordsize VM.
To expand on that, make an array of function pointers corresponding to your opcodes. Have the eval() function for a bytecode instruction use the first byte of the bytecode as the array index, and then call the function pointer at that position with the other two bytes as arguments. It's a pretty solid use case.
Also, you're not strictly incorrect--the function pointer does "close over" the execution environment of the system at that time, though that's more a feature of C and globals and pointer arithmetic than anything else. ;)
- Arelius 15y agoI'm not sure that closing over only global really counts...
- angersock 15y agoYes, I suppose it doesn't, as you can't really freeze the execution state (or the relevant parts of it) for the use of the closure--e.g., somebody else can change the memory values before your code gets run and so you act on stale data. :(