user7088945 user7088945 - 5 months ago 53
C++ Question

Different output at each run?

After I'm compiling the code

#include <iostream>

int* func(int a) { return &a; }

int main() {
std::cout << func(2) << std::endl;

when I run the executable multiple times, it returns different values at each run. This confuses me, I would have expected that the compiler removes all variable names and replaces them with fixed memory addresses, and therefore at each run of the executable I should have obtained the same value ?


Check out Address space layout randomization for one possible reason that the a variable gets stored at a different address each run.