r/C_Programming • • 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.

73 Upvotes

53 comments sorted by

View all comments

Show parent comment

3

u/devbent May 22 '26

This is super common in embedded. Fancy DMA engines on some MCUs let you do cool things with functional pointers as well.

On the other end of the things, Javascript is actually a function pointer based language at heart and you can do really evil things with FPs in it, tons of fun.