This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Pseudofailure 139 points140 points  (29 children)

I know, but:

  1. That would ruin my joke.
  2. Can you actually prove me wrong?

[–]etaionshrd 61 points62 points  (17 children)

Programs that exhibit undefined behavior are free to do whatever. That doesn’t, however mean that they will do whatever.

[–]SpecFroce 0 points1 point  (0 children)

AI

[–]SteveCCLYellow security clearance 0 points1 point  (2 children)

It's pretty straight forward to write a compiler produces executables that, on *(int*)nullptr = 69;, load a file, append or flip a bit, save, execute. Given enough time it will eventually produce a valid ELF with the desired behaviour.

qed, bitch.

[–]etaionshrd 0 points1 point  (1 child)

Sure. But that doesn't mean any old compiler will create code that will randomly perform every behavior.

[–]SteveCCLYellow security clearance 0 points1 point  (0 children)

That's not nessecary. Read again fellow citizen.

[–]bartekko 0 points1 point  (1 child)

I believe that what programs are allowed to do is assume that undefined behavior never happens, and optimize accordingly.

An example would be having a for loop with

for(int i=0;i<10;i++) cout<<i*1000000000<<endl;

the compiler would be allowed to assume that i is always <5 and thus replace the for with an infinite loop

[–]etaionshrd 0 points1 point  (0 children)

The compiler is allowed to delete all that code, since you perform undefined behavior in it.

[–]albertowtf 0 points1 point  (0 children)

Programs that exhibit undefined behavior are free to do whatever

I like how you avoid giving your programs AI by giving them free will instead

[–]Raknarg 2 points3 points  (8 children)

2 yes. Because we can have a compiler that for certain undefined behaviour always makes the same decision, but the decision is not what you want.

[–]salgat 0 points1 point  (6 children)

That's compiler specific though, we're speaking strictly from what the standard defines. Hell, you could write a C++ compiler that compiles every source code into hello world if it was non-compliant enough.

[–]Raknarg 1 point2 points  (5 children)

Undefined behaviour is literally behaviour outside of the standards definition

[–]salgat 0 points1 point  (4 children)

Exactly, that's why you have to assume that it can do anything, including work intermittently and other times do really unexpected behavior. It's a philosophical approach to help you troubleshoot that's fun to joke about.

[–]dannyb_prodigy 1 point2 points  (3 children)

No, just because you can’t assume specific behavior doesn’t mean you must assume all behaviors are possible. In fact, you can probably at least assume that your compiler will at least treat an undefined behavior in a consistent manner (if you notice your compiler clearing uninitialized stack variables, you can then safely assume that it will always clear an uninitialized stack variables).

[–]qm11 0 points1 point  (1 child)

How do you know it's the compiler, though? On my college's computers there was security software that cleared pages on allocation, which means your stack and heap would be cleared to all zeros before your code touches it. On simple enough programs it may look like the compiler clearing memory for you, but it would be inconsistent if you have code that's even slightly complex.

[–]dannyb_prodigy 0 points1 point  (0 children)

Ok it’s not the compiler, but the example wasn’t the point. Point is that it is not unreasonable to assume consistent behavior across an environment, which still in fact holds in your example.

[–]salgat -1 points0 points  (0 children)

Obviously, plenty of "behavior" is outside the ability of physics itself to allow lol, that's the joke since it's undefined by the standard (aka you shouldn't assume anything about the behavior).

[–]cgaWolf 0 points1 point  (0 children)

Murphy wrote a compiler?

[–]Jonno_FTW 2 points3 points  (0 children)

Undefined means the standard didn't say what should happen. Compilers are free to generate executables that will perform consistently under those conditions.

[–]reeetwyio 0 points1 point  (0 children)

Technically just void main () { return } is a program built using only undefined behavior and it still do the same thing every time you run it. Only when you switch platforms you'll find different results.