Undefined behavior - C backend

Just starting out? Need help? Post your questions and find answers here.
ricardo_sdl
Enthusiast
Enthusiast
Posts: 149
Joined: Sat Sep 21, 2019 4:24 pm

Undefined behavior - C backend

Post by ricardo_sdl »

Hi!
How does PureBasic C Backend compiler handles undefined behavior on the C generated code?
This PureBasic code:

Code: Select all

EnableExplicit

OpenConsole()

Define i.i = $7FFFFFFFFFFFFFFF

PrintN(Str(i))

i + 1

PrintN(Str(i))

Input()
Generated this C code in my test:

Code: Select all

// Define i.i = $7FFFFFFFFFFFFFFF
v_i=9223372036854775807;
// 
// PrintN(Str(i))
SYS_PushStringBasePosition();
SYS_PushStringBasePosition();
SYS_PushStringBasePosition();
PB_Str(v_i,SYS_PopStringBasePosition());
void* p0=(void*)SYS_PopStringBasePositionValueNoUpdate();
integer rr1=PB_PrintN(p0);
SYS_PopStringBasePositionUpdate();
// 
// i + 1
v_i=(v_i+1);
// 
// PrintN(Str(i))
Both backends (asm and c) executed and resulted in the same output:
9223372036854775807
-9223372036854775808
But depending on the C compiler used and compilation flags the result can be different. So how is it handled or is it we should look out for?
You can check my games at:
https://ricardo-sdl.itch.io/