`
weihe6666
  • 浏览: 444101 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

vs2010 下局部变量地址分配

阅读更多
  记得局部变量在栈中的地址是连续分配,今天在VS2010上发现局部变量在栈中的地址不是连续分配,查了一些资料,有的说是编译器的分配地址的算法不同而至。具体如下:
#include <iostream>
using namespace std;

int main()
{
	int iv;
	cout << "&iv " << &iv << endl;
	cout << "iv " << iv << endl;
	int iv2 = 1024;
	cout << "&iv2 " << &iv2 << endl;
	int iv3 = 999;
	cout << "&iv3 " << &iv3 << endl;
	int iv4 = 100,iv5 = 200,iv6 = 300;;
	cout << "&iv4 " << &iv4 << endl;
	cout << "&iv5 " << &iv5 << endl;
	cout << "&iv6 " << &iv6 << endl;
	int &reiv = iv2;
	cout << "&reiv= " << reiv <<endl;
	int &reiv2 = iv;
	cout << "&reiv2= " << reiv2 <<endl;
	int &reiv3 = iv;
	cout << "&reiv3= " << reiv3 <<endl;
	int *pi;
	cout << "&pi= " << pi <<endl;
	*pi = 5;
	cout << "*pi" << *pi << endl;
	pi = &iv3;
	const double di = 2.0;
	cout << "&di= " << &di <<endl;
	const double maxWage = 10.0;
	const double *pc = &maxWage;
	cout << "pc= " << pc <<endl;
	cout << "*pc= " << *pc <<endl;
	return 0;
}


输出:&iv 003DFBCC
     &iv1 003DFBC0 
     &iv2 003DFBB4
     &iv3 003DFBA8
     &iv4 003DFB9C
他们之间的地址相差12bytes 其中4bytes用于存储数据,8bytes用于存储其它
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics