r/C_Programming • u/Linguistic-mystic • Sep 19 '26
Question List all possible sources of non-linear control flow in C
Some people believe C is a language where statements are executed sequentially and there is no magic control flow. This is false, but I just want to make sure I'm aware of all possible sources of non-obvious control flow. This is what I've got:
- Signal handlers. The kernel can choose any thread, interrupt it and make it run a signal handler. This can be mitigated by signal masks (for example, having a dedicated thread to handle signals and masking all signals on all other threads) but by default C is non-linear.
longjmp. It interrupts the current call and maybe more calls, is not recoverable, does not perform any cleanup etc. And you have no control over it save for writing asetjmpyourself which still means your function gets interrupted. You could have asetjmpin every function to make 100% sure your control flow doesn't get hijacked, haha!- Exceptions from other languages. If a C function is called from C++ and calls a C++ function and that throws an exception... This only affects mixed code bases, though.
Is there anything else? Can we be sure that without these three sources a C function evaluates in the order the C standard prescribes?
66
Upvotes
6
u/torsten_dev Sep 19 '26
defer (TS 25755).
5
u/ern0plus4 Sep 19 '26
Interrupt handler on embedded platforms.
11
u/MagicWolfEye Sep 19 '26
I'm not sure what you want to say with the longjmp
You say
longjmpand it goes to the correspondingsetjmp3
u/spellstrike Sep 19 '26 edited Sep 19 '26
reading from volatile variables related to hardware values that can change at any time?
really anything once you start dealing with an embedded system with error handling, callbacks, embedded multi threading stars to get to the point that it's no longer just doing a ordered list of commands
47
u/josk613 Sep 19 '26
goto, atexit
41
u/OCROttawa Sep 19 '26
Your subject line talks about non-linear and then you ask for examples of non-obvious control flow. I would venture that these are not the same thing and neither is what you are asking about. The other comment I would make is that you need to differentiate the programming language “C” and the underlying system, be it an operating system or even bare metal.
“C” the language is by definition deterministic, it is not linear. If it was non-deterministic (whatever that means in this case) it would be very hard to program.
A loop is non-linear, a subroutine call is non-linear, both are deterministic and obvious. Have you ever looked at the way a case statement ends up when it is compiled into assembler, jumps all over the place.
You mention threads and kernels, these are operating system constructs. Signal/interrupt handlers are the same. Fork() is not a “C” construct it is an operating system call, if your OS doesn’t support fork() then the function will not exist.
Even things you are calling “non-linear”, like signal handlers are 100% deterministic.
There is nothing in the “C” language that is being violated by these “non-linear” operating system functions, if they were then the system would fall apart.
I guess the question still exists…what it is that you are trying to understand? Or what are you worrying about?
18
u/Dezgeg Sep 19 '26
Not in standard C but setcontext() and similar come to mind.
15
u/Cats_and_Shit Sep 19 '26 edited Sep 19 '26
This is incorrect.
longjmpdoes not nececarily take you to the most recentsetmpcall, it takes you to the one that initialized the providedjmp_buf envargument.10
u/HugoNikanor Sep 19 '26
fork(2)(while technically not part of C) is interesting, since the following instructions are executed independently on two separate threads.3
u/ByMeno Sep 19 '26
Unspecified evaluation order and short-circuiting can count i think ?
Edit: Maybe exit handlers too
3
u/TheThiefMaster Sep 19 '26
OS exceptions - e.g. Structured Exceptions on Windows
3
u/Cats_and_Shit Sep 19 '26 edited Sep 19 '26
Yes, but only on a technecicality, since the C standard does not prescribe any evaluation order.
The standard requires that externally visible operations, like interactions with files or volitile memory, occur as if a function was evaluated as a series of sequential statements. But neither your compiler nor you CPU will actually do that, because it would make things much, much slower.
2
u/actguru Sep 19 '26
There are compiler extensions like GCC
__attribute__((cleanup(function_name))) that cause scope related execution.2
u/wiskinator Sep 19 '26
Not just signal handlers but interrupt handlers too.
2
u/sciencekm Sep 20 '26
Overrun your stack and the function returns somewhere else (assuming it does not outright crash).
3
u/duane11583 Sep 19 '26
As a language The c language does not allow non linear flow
Your example of a signal handler is misleading. When the signal is delivered it starts at the function entry point
And setjmp longjmp is just an extra return with nonzero return value but it otherwise uses the same control flow
What would be non linearity could occur during a stack over flow where the return address gets clobbered and the code returns somewhere in expected but then the code would continue in a linear fashion
At some basic level the cpu is executing the next code in a linear (one after another) way
So I’m not following what you mean
2
u/maep Sep 19 '26
All guarantees go out the window when you invoke undefined behavior.
1
u/realdreamer1993 Sep 20 '26
there is hierarchy, and our program/process is owned by other entity.
So the entity usually have some ways to intervense our process...but still we 99% focus on the C source code.
We don't care/program the other entity except on the boundary/contract.
1
u/TheOtherBorgCube Sep 20 '26
Threads since C11 https://en.cppreference.com/c/header/threads
Various thread libraries before then.
thrd_createmight be entirely deterministic in terms of you call it, it returns.But the side-effect is that now you have something else going on in some parallel fashion with your current code.
1
u/burlingk Sep 20 '26
C programs are pretty much assumed to be built with if then and switch statements.
So whoever you're talking to probably needs to study, at all... Because more would imply the already tried.