I am running the following program in code blocks with GNU GCC as compiler on windows platform. Here in this program I want to check whether integer declared in function changes memory location. I know due to compiler optimization it may happen sometime that it will keep same address for variable even if it declared multiple times. But I am calling it million times then also it takes same address. I also tried it with volatile keyword even with volatile keyword it shows same output.
#include <iostream>
using namespace std;
int* test (int a, int b)
{
int c=0;
c=c+a+b;
return &c;
}
int main()
{
int* pre;
pre = test(5,9);
int i=0;
for( i=0;i<1000000;i++)
{
int* cur = test(i,i+6);
if(cur!=pre)
{
cout<<"wrong";
}
}
cout<<i;
return 0;
}