gpt4 book ai didi

c++ - C++指针的地址

转载 作者:行者123 更新时间:2023-12-04 00:48:58 27 4
gpt4 key购买 nike

#include <iostream>

using namespace std;

int main(int argc, char** argv) {
unsigned int a =5;
unsigned int *pint = NULL;

cout << "&a = " << &a << endl;
cout << " &pint = " << &pint << endl;
}

输出:

&a = 0x6ffe04
&pint = 0x6ffdf8

我想知道为什么 pint 的地址等于 0x6ffdf8pint是一个unsigned int(4字节),它的地址不应该是0x6ffe00吗?

最佳答案

您的 pint 不是无符号整数。它是一个指向无符号整数的指针。
指针可以有不同的大小,尤其可以有 8 的大小。
因此它可以在 0x6ffe04 之前放入 0x6ffdfc。
但它也有更大的对齐需求,它需要一个可被 8 整除的地址,所以 0x...c 不存在,它需要例如0x...8

对于 463035818_is_not_a_number,我同意这不是真正可预测的,存在特定于实现的方面。这就是为什么我用“可以”、“想要”、“例如”来表达“轻轻地”......

关于c++ - C++指针的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68255070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com