user7088945 user7088945 - 1 year ago 95
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 ?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download