全局变量栈内存分布



class AA{ public: int a; AA(){ cout << __FUNCTION__ << endl; } };
class BB{ public: char a; BB(){ cout << __FUNCTION__ << endl; } };
class CC{ public: int a; CC(){ cout << __FUNCTION__ << endl; } };
class DD{ public: char a; DD(){ cout << __FUNCTION__ << endl; } };
class EE{ public: char a; EE(){ cout << __FUNCTION__ << endl; } };
class FF{ public: char a; FF(){ cout << __FUNCTION__ << endl; } };
class GG{ public: char a; GG(){ cout << __FUNCTION__ << endl; } };
class HH{ public: char a; HH(){ cout << __FUNCTION__ << endl; } };






int a;
char b;
int c;
char d;
int e;
char f;
char g;
char h;
 
AA aa;
BB bb;
CC cc;
DD dd;
EE ee;
FF ff;
GG gg;
HH hh;





int main(...)
{

	cout << (unsigned int)&a - (unsigned int)&a << endl;
	cout << (unsigned int)&b - (unsigned int)&a << endl;
	cout << (unsigned int)&c - (unsigned int)&a << endl;
	cout << (unsigned int)&d - (unsigned int)&a << endl;
	cout << (unsigned int)&e - (unsigned int)&a << endl;
	cout << (unsigned int)&f - (unsigned int)&a << endl;
	cout << (unsigned int)&g - (unsigned int)&a << endl;
	cout << (unsigned int)&h - (unsigned int)&a << endl;
 
	cout  << endl;

	cout << (unsigned int)&aa - (unsigned int)&aa << endl;
	cout << (unsigned int)&aa - (unsigned int)&bb << endl;
	cout << (unsigned int)&aa - (unsigned int)&cc << endl;
	cout << (unsigned int)&aa - (unsigned int)&dd << endl;
	cout << (unsigned int)&aa - (unsigned int)&ee << endl;
	cout << (unsigned int)&aa - (unsigned int)&ff << endl;
	cout << (unsigned int)&aa - (unsigned int)&gg << endl;
	cout << (unsigned int)&aa - (unsigned int)&hh << endl;
 
	cout<< endl;



	int aaa;
	char bbb;
	int ccc;
	char ddd;
	char eee;

	cout << (unsigned int)&aaa - (unsigned int)&aaa << endl;
	cout << (unsigned int)&aaa - (unsigned int)&bbb << endl;
	cout << (unsigned int)&aaa - (unsigned int)&ccc << endl;
	cout << (unsigned int)&aaa - (unsigned int)&ddd << endl;
	cout << (unsigned int)&aaa - (unsigned int)&eee << endl;
 

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/751770

JVM(Java虚拟机)是Java程序的运行环境,负责将Java字节码转换成机器码,并在内存中执行。在JVM中,局部变量和全局变量在内存中有不同的分布方式。 局部变量是定义在方法内部或代码块内部的变量,它的生命周期只存在于方法或代码块执行期间。当方法或代码块执行结束后,局部变量就会被销毁。局部变量的存储分布栈内存中。 栈内存是JVM中用来存储方法调用的数据和局部变量的地方。当一个方法被调用时,JVM会为该方法创建一个帧,帧中包含了方法参数、返回值、局部变量以及方法执行的状态信息。局部变量在帧中以变量的形式存储,并且在方法调用结束后帧会被销毁,从而局部变量也会被自动释放。 全局变量是定义在类或接口中的变量,被整个类或接口所共享,其作用域在整个类或接口内部可见。全局变量的存储分布在堆内存中。 堆内存是用于存储JVM中的对象数据的地方。当使用new关键字创建一个对象时,对象会被分配在堆内存中,并且全局变量也是存储在堆内存中。全局变量在对象创建后会一直存在,直到对象被垃圾回收器回收时才会销毁。 总结来说,局部变量的存储分布栈内存中,随着方法或代码块的执行周期而存在和销毁;而全局变量的存储分布在堆内存中,随着对象的创建和销毁而存在和销毁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值