What happen when program reach unreachable on ReleaseFast? (Zig lang)

426 Views Asked by At

I read on Zig Doc it has undefined behavior. is that it? isn't there any way tho predict the behavior of the code after hitting unreachable? like if it's process next line or try to continue like unreachable never been there!

2

There are 2 best solutions below

0
On BEST ANSWER

isn't there any way tho predict the behavior of the code after hitting unreachable? like if it's process next line or try to continue like unreachable never been there!

No, the compiler couldn't optimize as much otherwise.

Never use unreachable if you want to control what happens.

Remember: UB is worse than unpredictability when you hit that line. The optimizer could break your program even before you arrive at the point you hit UB.

0
On

That's it. If you could guarantee how the code would behave in any given scenario, that would be defined behavior.

If you want to know why undefined behavior exists, see here for a start.