5 ms·
> If 'b' is actually used then the compiler has to put 'a' in memory. Theoretically, no—the “as if” rule applies. In practice, also no, because real compilers
by klodolph 6y ago
> If 'b' is actually used then the compiler has to put 'a' in memory.
Theoretically, no—the “as if” rule applies. In practice, also no, because real compilers perform escape analysis and there are aggressive optimization passes for eliminating dead loads and dead stores.
Just like the compiler can optimize out b and replace it with a reference to a, the compiler can optimize out the value stored in a but keep the reference to it stored in b.
> In that case b is useless, and thus it doesn't really even exist.
I don’t think this notion of “exist” has legs.
- gens 6y agoThat is what i said, that it can be optimized away. "exist" is the wrong word, "is useless" would be better. It is like.. you have a spanner in a box labeled "a" and you have a box "b" that has a piece of paper that says "look in a". You ask for a spanner and you get told to look in box "a". The box labeled "b" is then completely useless. One day some guy, weirdly named "compiler", decides to trow away box "b", and nobody cares. But we have to assume that a compiler will compile the program just as we wrote it. Otherwise we are not talking about the language, but about the compiler. This is gone off topic, if there even was one.
- klodolph 6y agoA variable is useless because it is optimized away? I don’t agree with that terminology. The variable is useful / exists in the source code, and that is enough. Variables / objects only exist conceptually in the original program anyways, we just infer their existence in the compiled program by correspondence with the original code, or educated guesses about the original code.