程序在栈中分配变量的不连续
/*
如下代码,我发现打印出来的对变量分配的内存地址不连续,存在部分空白,能说明一下程序的分配机制吗?
*/
#include <iostream>
using namespace std;
int main()
{
int i = 15;
int j = 18;
char c1 = 'A',c2 = 'B';
int f1 , f2;
char c3;
int i1;
cout<<"&i:"<<&i<<endl
<<"&j:"<<&j<<endl;
cout<<"&c1:"<<(void *)(&c1)<<endl
<<"&c2:"<<(void *)(&c2)<<endl;
cout<<"&f1:"<<&f1<<endl
<<"&f2:"<<&f2<<endl;
cout<<"&c3:"<<(void*)(&c3)<<endl;
cout<<"&i1:"<<&i1<<endl;
return 0;
}
/*我的显示结果如下:
&i:0x22ff74
&j:0x22ff70
&c1:0x22ff6f
&c2:0x22ff6e
&f1:0x22ff68
&f2:0x22ff64
&c3:0x22ff63
&i1:0x22ff5c
分析:
从
&c2:0x22ff6e
&f1:0x22ff68 之间的分配明显多了2个字节,
从
&c3:0x22ff63
&i1:0x22ff5c 之间的分配则多了3个字节,
貌似在栈中是按照4个字节为单位进行分配的,为什么?
*/