Last active
June 18, 2020 12:44
-
-
Save wukaihua119/941b309a6ccb71827f02510e6c4fa2b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstdio> | |
struct a{ | |
int val; | |
}; | |
int main(){ | |
/* Pointer's calculation: | |
* | |
* 指標的運算是記憶體位址的位移, 根據"指向的型態"大小位移量而有所不同, | |
* int為4 bytes, char為1 byte, pointer為8 bytes. | |
* | |
*/ | |
// a1, a2, a3皆指向struct, 這裡為4bytes | |
a *a1 = new a; | |
a1->val = 5; | |
a *a2 = new a; | |
a2->val = 3; | |
a *a3 = new a; | |
a3->val = 4; | |
// arr存放指向整數的pointer | |
a *arr[3] = { a1, a2, a3 }; | |
// pa指向pointer | |
a **pa = arr; | |
std::cout << "Using arr:\n"; | |
std::printf( "1st val: %d\n", arr[0]->val ); | |
std::printf( "1st pos: %d\n", arr[0] ); | |
std::printf( "2nd val: %d\n", arr[1]->val ); | |
std::printf( "2nd pos: %d\n", arr[1] ); | |
std::cout << std::endl << std::endl; | |
std::cout << "Using pa:\n"; | |
std::printf( "1st val: %d\n", (*pa)->val ); | |
std::printf( "1st pos: %p\n", (*pa) ); // pa的內容物為指向struct的pointer a1的值, 此值是指向heap的一個位址 | |
std::printf( "2nd val: %d\n", (*(pa+1))->val ); // 指向arr2的內容物 | |
std::printf( "2nd pos: %p\n", *(pa+1) ); // *(pa+1)表示pa位移其所指向的型態arr, 因此位移一個arr的大小後取出內容物a2的值 | |
std::printf( "2nd pos: %p\n", *(pa)+1 ); // *(pa)+1表示取出pa指向的位址arr[0]之內容物a1, a1是指向struct型態, 並未一個向右位移4 bytes. | |
std::cout << "size of (pa) = " << sizeof( pa ) << "\n"; | |
std::cout << "size of *(pa) = " << sizeof( *(pa) ) << "\n\n"; | |
std::cout << std::endl << std::endl; | |
a *pa1 = *(pa); // pa1為pa指向arr位址的內容物a1 | |
std::cout << "Using pa1:\n"; | |
std::printf( "1st val: %d\n", pa1->val ); | |
std::printf( "1st pos: %p\n", pa1 ); // 實際上就是指向a1 | |
std::printf( "2nd val: %d\n", (pa1+1)->val ); | |
std::printf( "2nd pos: %p\n", pa1+1 ); // a1巷右邊移動一個struct大小的位址, | |
std::cout << "size of pa1 = " << sizeof( pa1 ) << "\n\n"; | |
// any type of pointer has size of 8 bytes. | |
std::cout << sizeof( a1 ) << std::endl; // 8 | |
std::cout << sizeof( arr ) << std::endl; // 8 * 3 = 24 | |
std::cout << sizeof( *(arr+1) ) << std::endl; // 8 | |
std::cout << sizeof( pa ) << std::endl; // 8 | |
std::cout << sizeof( pa1 ) << std::endl; // 8 | |
std::cout << sizeof( a ) << std::endl; // 4 | |
std::cout << sizeof( int * ) << std::endl; // 8 | |
std::cout << sizeof( int ) << std::endl; // 4 | |
std::cout << std::endl << std::endl; | |
delete a1, a2, a3; | |
int b1 = 1, b2 = 2, b3 = 3; | |
int *barr[3] = { &b1, &b2, &b3 }; | |
int **pbarr = barr; | |
int *ppbarr = *pbarr; | |
std::cout << sizeof( b1 ) << std::endl; // 4 | |
std::cout << sizeof( barr ) << std::endl; // 24 | |
std::printf( "&b1 = %d\n", &b1 ); | |
std::printf( "barr[0] = %d\n", barr[0] ); | |
std::printf( "*pbarr = %d\n", *pbarr ); | |
std::printf( "*(pbarr+1) = %d\n", *(pbarr+1) ); | |
std::printf( "barr[1] = %d\n", barr[1] ); | |
std::printf( "(*pbarr)+1 = %d\n", (*pbarr)+1 ); | |
std::printf( "*(ppbarr+1) = %d\n", *(ppbarr+1) ); | |
std::printf( "ppbarr+1 = %d\n", ppbarr+1 ); | |
std::printf( "(pbarr) = %d\n", (pbarr) ); | |
std::printf( "(pbarr+1) = %d\n", (pbarr+1) ); // 移動8個bytes | |
return 0; | |
} | |
/* | |
Using arr: | |
1st val: 5 | |
1st pos: 0x559bbf0bee70 | |
2nd val: 3 | |
2nd pos: 0x559bbf0bee90 | |
Using pa: | |
1st val: 5 | |
1st pos: 0x559bbf0bee70 | |
2nd val: 3 | |
2nd pos: 0x559bbf0bee90 | |
size of (pa) = 8 | |
size of *(pa) = 8 | |
Using pa1: | |
1st val: 5 | |
1st pos: 0x559bbf0bee70 | |
2nd val: 0 | |
2nd pos: 0x559bbf0bee74 | |
size of pa1 = 8 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment