changed address of variable in the heap segement

#include<bits/stdc++.h>
using namespace std;
int* A( ){
int* a = new int;
*a = 3;
cout<<a;
return a;
}
int main( ){
A( ); // the output is 0x1c1540

cout<<endl;
cout<<*A( ); // the output is 0x1c15603
} // why does the address of a changed?
why does the address of a changed?
Because each time you call A() new memory for the local variable a is allocated.

By the way: you create a memory leak because you never free the memory allocated per call.
Topic archived. No new replies allowed.