Hacker News new | ask | show | jobs
by wcummings 4209 days ago
>It is sinful to jump into a different function

So sinful you can't in C

3 comments

You can do it in GNU C! It's used to write direct-threading interpreters.

https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Labels-as-Values....

They don't intend for label addresses to be gone to outside of the function they are located in. The stack wouldn't be setup to handle them at all.

Sure, you can store them wherever, and do something like " goto *g_greenthreads[ threadno ].lastpos ", but you would call that within the function defining the label, not outside it.

From your own link: > You may not use this mechanism to jump to code in a different function. If you do that, totally unpredictable things will happen. The best way to avoid this is to store the label address only in automatic variables and never pass it as an argument.

I assumed the parent poster meant back before functionality was expressed as functions; i.e. it's sinful to jump from functionality A to functionality B if it's not expressed in terms of packaged functions, and impossible to do so if it is.
With longjmp you can. Your stack will be trashed, though, so any state better be global!