-1

My actual concern is about what happens if I write an expression that causes the assignment of a value to a variable, in which the value I want to assign has already been stored.

For Example:

#include <stdio.h>

int main(void)
{
     int var = 1;

     printf("The actual value of var is %d",var);

     var = 1;                                  // What happens exactly if I bring in this expression?
                                               // Does it rewrite the memory?
     return 0;
}

Does it rewrite the value of 1 to var in the memory and is this causing longer run-time?

Or does it just seemingly skip the assignment command anyhow?


I´ve searched for an exact answer but I couldn't find one inside questions already made here, as well as not in C99 at my sight.

The question is for C and C++, as I work with both and I didn´t want to make the same question twice. If the answer between those two alternatives, please mention which language is in focus.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Even in the worst case, do you really expect to observe actual difference, with modern multi-Ghz CPUs, and RAM? P.S. In this case nothing happens because the compiler will obviously optimize the redudant assignment away. – Sam Varshavchik Nov 26 '19 at 12:01
  • Since you can't tell the difference, the question isn't really meaningful. You are asking which of two indistinguishable things happens. – David Schwartz Nov 26 '19 at 12:02
  • 2
    Why is this a concern? Have you measured variable assignments to be a bottleneck in your program? Don't bother with micro-optimizations (especially basically unmeasurable ones like this) until you solved all top-ranking bottlenecks. – Some programmer dude Nov 26 '19 at 12:02
  • 1
    I don't see what's wrong with the question. Don't vote down for clear and properly formatted questions only if you feel it is "stupid". – arrowd Nov 26 '19 at 12:10
  • @Someprogrammerdude It is just a theoretical question. I know, that the program will not sigificantly be delayed by this assignment. Just a question about the source. – RobertS supports Monica Cellio Nov 26 '19 at 12:11
  • Asking for curiosity is all well and fine. But this doesn't really involve C or C++ as neither specification includes optimizations on such levels. And it's also very dependent on compilers and even specific versions of compilers, as well as context (surrounding code). So there's really no simple "yes" or "no" answer here, which makes it to broad. – Some programmer dude Nov 26 '19 at 12:15

1 Answers1

5

Let's try that here:

enter image description here

As you can see, this compiler reassigns the value. The var = 1; statement translates to a mov instruction. Now let's try that with a higher optimization level:

enter image description here

Now the var = 1; doesn't translate to any assembly. It has been optimized away. Even the int var = 1; has been optimized away, the value of 1 is now hardcoded for that printf call.

Generally it depends on the compiler, the options, the language and possibly lots of other things. Nowadays a modern compiler will often optimize such code, but if you want to be sure, you should always try it yourself.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    It depends in particular on the surrounding and intermediate code. If you e.g. insert a call to a function whose definition is not available taking `var` by reference, then the compiler cannot prove anymore that the second store can be eliminated without change in behavior. For a global variable, any function call without available definition would hinder elimination, etc.. – walnut Nov 26 '19 at 12:08
  • 1
    but then the definition of `printf` is known (as per it would be UB if it doesn't do what the compiler can assume) – Antti Haapala -- Слава Україні Nov 26 '19 at 12:15
  • @AnttiHaapala True, it also doesn't take `var` by reference or pointer. And in this particular case the last store's value is never used anyway, so it can always be optimized out. Hence the surrounding and intermediate code is relevant. [Here](https://godbolt.org/z/e5dx23) is an example where the store is not eliminated because gcc doesn't use the fact that the definition of `printf` is always known not to modify the pointed to value passed to it. – walnut Nov 26 '19 at 12:20