#include <iostream>
#include <stdio.h>
using namespace std;
int d_ = 1;
int s_ =2;
class A{
};
int main()
{
A *d = new A();
A *s = new A();
cout<<&d<<” “<<&s<<endl;
int i = 0;
int j = 1;
cout<<&i<<” “<<&j<<endl;
cout<<&d_<<” “<<&s_<<endl;
return 0;
}![C++中内存分配问题 C++中内存分配问题](https://www.codebye.com/wp-content/uploads/2016/08/-2752910032017295164.jpg)
#include <stdio.h>
using namespace std;
int d_ = 1;
int s_ =2;
class A{
};
int main()
{
A *d = new A();
A *s = new A();
cout<<&d<<” “<<&s<<endl;
int i = 0;
int j = 1;
cout<<&i<<” “<<&j<<endl;
cout<<&d_<<” “<<&s_<<endl;
return 0;
}
![C++中内存分配问题 C++中内存分配问题](https://www.codebye.com/wp-content/uploads/2016/08/-2752910032017295164.jpg)
解决方案
40
d 和 s 都是局部变量,被分配在栈上,它们分别指向堆上的不同对象