加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 指针内存分配

发布时间:2020-12-16 10:11:07 所属栏目:百科 来源:网络整理
导读:我试图深入学习指针的概念.在下面的代码中,我创建了一个数组,创建了一个指向每个元素的指针. int bucky[5];int *bp0 = bucky[0];int *bp1 = bucky[1];int *bp2 = bucky[2];cout"bp0 is at memory address:"bp0endl;cout"bp1 is at memory address:"bp1endl;c
我试图深入学习指针的概念.在下面的代码中,我创建了一个数组,创建了一个指向每个元素的指针.

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;

这些是给予数组元素的内存分配.

bp0 is at memory address:0x0018ff3c
bp1 is at memory address:0x0018ff40
bp2 is at memory address:0x0018ff44

由于我对c的知识有限,我知道内存是连续分配给一个数组的.但仔细观察输出,bp0看起来不合适.

据我说bp0应该在0x0018ff36.或者是CPU中的位置0x0018ff3c,0x0018ff40,0x0018ff44是连续的吗?

那么有可能在一个进程中没有分配两个连续的内存分配吗?

解决方法

+---+---+---+---+---+---+---+---+---+---+---+---+
           |      bp0      |      bp1      |      bp2      |
           +---+---+---+---+---+---+---+---+---+---+---+---+
  0x0018ff3c   d   e   f  40   1   2   3   4   5   6   7   8

假设int的大小是4个字节,并且bp0指向0x0018ff3c.

bp1 = bp0 4 = 0x0018ff40bp2 = bp1 4 = 0x0018ff44

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读