r/C_Programming • u/TessaFractal • May 21 '26
Question Are runtime created functions possible? (Cursed ideas)
This sounds like a bad idea, but I'm curious if it's even possible. But imagine you have a function that needs different parts of it turned on and off depending on user selected parameters. Instead of a bunch of if statements for each part, can you create the resultant function needed at runtime, and just call that, skipping the need for if statements at every call?
I feel like you could do a list of function pointers, and call them all in sequence, and that also sounds cursed, and not a thing I've even heard of.
But wondering if a solution like: copying the machine code for a function into memory, and then casting the pointer as a pointer to function and calling it, is that even possible.
View all comments
Show parent comment
2
u/gm310509 May 22 '26
No worries. I am curious, based upon the replies to your post, what is your feeling now?
While probably not completely relevant to you, in one of my how to videos, I do actually use function pointers to invoke functions indirectly based upon user input (if you are interested).