Moh*_*asi 3 c c++ memory arrays pointers
我试图深入学习指针的概念.在下面的代码中,我创建了一个数组,创建了一个指向每个元素的指针.
int bucky[5];
int *bp0 = &bucky[0];
int *bp1 = &bucky[1];
int *bp2 = &bucky[2];
cout<<"bp0 is at memory address:"<<bp0<<endl;
cout<<"bp1 is at memory address:"<<bp1<<endl;
cout<<"bp2 is at memory address:"<<bp2<<endl;
Run Code Online (Sandbox Code Playgroud)
这些是给予数组元素的内存分配.
bp0在内存地址:0x0018ff3c
bp1在内存地址:0x0018ff40
bp2在内存地址:0x0018ff44
由于我对c ++的了解有限,我知道内存是连续分配给一个数组的.但仔细观察输出,bp0看起来不合适.
据我说bp0应该在0x0018ff36.或者是0x0018ff3c , 0x0018ff40 , 0x0018ff44CPU 中的位置是连续的?
那么有可能在一个进程中没有分配两个连续的内存分配吗?
+---+---+---+---+---+---+---+---+---+---+---+---+
| bp0 | bp1 | bp2 |
+---+---+---+---+---+---+---+---+---+---+---+---+
0x0018ff3c d e f 40 1 2 3 4 5 6 7 8
Run Code Online (Sandbox Code Playgroud)
假设int的大小是4个字节,并且bp0指向0x0018ff3c.
bp1 = bp0 + 4 = 0x0018ff40
bp2 = bp1 + 4 = 0x0018ff44