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

标签 c++ pointers x86-64 memory-address

#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/

相关文章:

c++ - 为什么我需要两次使用这个语句 - 矩阵乘法

c++ - 为什么cc可以编译c++程序?

c - 从 C 链表中删除(指针到指针)

c - 函数指针使用的地址

performance - 随机内存写入比随机内存读取慢?

Python ctypes 和函数调用

c++ - 卷积 : the last element in the convolution is never right

c++ - 虚拟继承中最基类是否需要默认ctor?

c - 错误调用的对象 'calculateTaxes'不是函数或函数指针

c - 在结构数组内分配指针