Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The address of C ++ pointer

#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;
}

Output:

&a = 0x6ffe04
&pint = 0x6ffdf8

I'm wondering why the address of pint equals 0x6ffdf8. pint is an unsigned int (4 bytes), shouldn't the address of it be 0x6ffe00?

like image 237
JOya Avatar asked Nov 17 '25 05:11

JOya


1 Answers

Your pint is not an unsigned int. It is a pointer to an unsigned int.
Pointer can have a different size and can especially have the size 8.
It could hence fit into 0x6ffdfc before 0x6ffe04.
But it also has bigger alignment needs, it wants an address dividable by 8, so 0x...c is out, it needs e.g. 0x...8.

With 463035818_is_not_a_number I agree that this not really predictable, there are implementation specific aspects. That is why I phrase "softly" with "can", "wants", "e.g."....

like image 116
Yunnosch Avatar answered Nov 19 '25 19:11

Yunnosch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!