简体   繁体   中英

Understaning pointers & memory management

I am trying to understand pointers in C++.

There we have a little example of code:

int main(void) {
    int var = 8;
    void* pointer = &var; // 0x00A0FB64
}

And there is an image of memory: enter image description here

0x00A0FB64  08 00 00 00 cc cc cc cc ca c8 a0 1d 84 fb a0 00 ee 2b bf 00 01 00 00 00 20 53 12 01 00 5a 12 01 dc fb a0 00 50 2a bf 00

There is 08 00 00 00 in hexadecimal numeral system and when we convert it to binary numeral system:

We will get 1000 00000000 00000000 00000000 .

The size of integer should be 32 bits, but there are only 28 bits.

How is it possible?

And do I get it right the table of memory of integer is (?) :

0x00A0FB64 -> 08
0x00A0FB65 -> 00
0x00A0FB66 -> 00
0x00A0FB67 -> 00

Thanks for any help!

08 is 1000 in binary. However, so is 00001000 .

Thus, the memory actually becomes: 00001000 00000000 00000000 00000000 , which is 32 bits.


Side issue, there technically are no guaranteed sizes in C++ , only that int is at least 16 bits (among other guarantees for other types).

So, in theory, it could be 28 bits, as long as all other requirements for type sizes are fulfilled. It is merely in practice that you probably won't have 28 bits, but more likely 32.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM