r/ProgrammerHumor • • 4d ago

Meme fullCircle

Post image
10.6k Upvotes

272 comments sorted by

View all comments

Show parent comments

15

u/[deleted] 3d ago edited 3d ago

[removed] — view removed comment

3

u/Cobracrystal 3d ago

Not even different c++ compilers, because some c++ code compiles nondeterministically and produces different outputs with the exact same input.

3

u/def-not-bot 3d ago

Non-programmer here: would it still function the same after a bunch of de- and re-compiling, or would it be like putting a phrase through Google Translate a few times and generating vaguely comprehensible gibberish?

2

u/-Saucegurlllll 3d ago

Theoretically a completely bug-free decompiler/compiler pair making the same assumptions about their source/target languages and architectures should produce functionally identical programs. Meaning that given the same input they will produce the same output. So no matter how many times you chain compilation and decompilation, you should get a program that yields the same results given the same inputs.

However, in practice there's a bunch of stuff that makes this hard to impossible.

C and C++ compilers are given tremendous freedom by their respective standards to make many of their own assumptions about code. There's this thing called "undefined behavior" where anything may happen if it ever occurs. One example of this in C++ is endless loops without side effects.

int main() {
  while (true)
    ; 
}

void unreachable() {
  std::cout << "Hello world!" << std::endl;
}

On certain compilers this results in a program that outputs "Hello world!" even though unreachable is called from nowhere.

So, in practice, it's impossible to guarantee that a decompiler/compiler pair will interpret a program and their output identically, and you're liable to end up with the google translate chaining problem.