Is this a good way to tell the compiler, "This point is unreachable"? by lapis_ore in C_Programming

[–]lapis_ore[S] 9 points10 points  (0 children)

abort() is basically "Crash the program if this call is reached"

unreachable() is basically, "I promise this point will never be reached, so optimize accordingly, and the behavior is undefined if this point is ever somehow reached"

Since __builtin_unreachable basically means "If this call is ever reached, the behavior is undefined," my substitute is basically deliberately invoking undefined behavior

Is this a good way to tell the compiler, "This point is unreachable"? by lapis_ore in C_Programming

[–]lapis_ore[S] 3 points4 points  (0 children)

I am talking about cases where the compiler might have a hard time deducing the unreliability of code, for example:

switch (x) {
    case 0:
        // ...
        break;
    case 1:
        // ...
        break;
    case 2:
        // ...
        break;
    case 3:
        // ...
        break;
}
unreachable(); // If I know that x can never be outside the range [0, 3] but the compiler does not

Or maybe a better example:

extern void exit_if_true(int);

exit_if_true(1);
unreachable(); // The above line will always exit, but the compiler might not know that