Pointer&Reference

Cpp Operation Note

不同於Python及Java, c++同時支援Pointer及Reference。


名詞解釋

  1. Value (值): 記憶體內儲存的變數
  2. Pointer (指標): 存儲變數之記憶體的絕對位置
    *Pointer會另開記憶體存取地址。
  3. Reference (引用): 調出變數所存在記憶體之絕對位置
    *Reference導向所附記憶體位置,不另開記憶體。
  4. 操作符 (&): 取變數所在之記憶體絕對位置。
  5. 操作符 (*): 對記憶體位置取出值。

初始化

  1. Pointer初始化:
1
2
3
4
5
6
7
8
9
10
11

int a = 5;
int *ptr = &a;

//or

int *ptr;
ptr = &a;

//ptr = 0x------------;
//*ptr = 5;
  1. Reference初始化:
1
2
3
4
5
6
7
8
9
10
11
12
int a = 5;
int &ref = a;

//but

int &ref;
ref = a; // This operation is incorrect.

int *ptr = &a;

//ref = *ptr = 5;
//&ref = ptr = 0x------------;
  1. 重新附值:
1
2
3
4
5
6
int a = 3;
int b = 5;
int *ptr;

ptr = &a;
ptr = &b;

Pointer可以重新附值

1
2
3
4
5
int a = 3;
int b = 5;

int &ref = a;
int &ref = b;//This wil throw an error of "multiple declaration is not allowed"

Reference不可被重新附值及宣告


實裝例swap

1. int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

void swap_int(int a, int b){
swap(a, b);
cout << "In function:" << endl;
cout << " a = " << endl;
cout << " b = " << endl;
}
int main(){
int a = 1 ;
int b = 2 ;
swap_int(a, b);
cout << "In main:" << endl;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
return 0 ;
}

output

1
2
3
4
5
6
in function:
a = 2
b = 1
in main:
a = 1
b = 2

交換失敗,原因是a,b在main及function中為兩獨立區域變數,不互相干擾。


2. *int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

void swap_value_ptr(int *ptra, int *ptrb){
swap(*ptra, *ptrb);
}

int main(){
int a = 1 ;
int b = 2 ;
cout << "Before swap:" << endl;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
cout << " &a = " << &a << endl;
cout << " &b = " << &b << endl;
swap_value_ptr(&a, &b);
cout << "After swap:" << endl;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
cout << " &a = " << &a << endl;
cout << " &b = " << &b << endl;
return 0 ;
}

將a,b記憶體位置傳入function,並交換兩記憶體內value

output

1
2
3
4
5
6
7
8
9
10
Before swap:
a = 1
b = 2
&a = 0x7ffcdb13f900
&b = 0x7ffcdb13f904
After swap:
a = 2
b = 1
&a = 0x7ffcdb13f900
&b = 0x7ffcdb13f904

從output可以看出a,b變數之記憶體維持而變數value交換。


3. &int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

void swap_ref(int &refa, int &refb){
swap(refa, refb);
}

int main(){
int a = 1 ;
int b = 2 ;
cout << "Before swap:" << endl;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
cout << " &a = " << &a << endl;
cout << " &b = " << &b << endl;
swap_ref(a, b);
cout << "After swap:" << endl;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
cout << " &a = " << &a << endl;
cout << " &b = " << &b << endl;
return 0 ;
}

原理同上一個,改用引用方式調換value

output

1
2
3
4
5
6
7
8
9
10
11
Before swap:
a = 1
b = 2
&a = 0x7ffd068869c0
&b = 0x7ffd068869c4
1
After swap:
a = 2
b = 1
&a = 0x7ffd068869c0
&b = 0x7ffd068869c4

從output可以看出a,b變數之記憶體維持而變數value交換。

參考資料: [C++基礎]019_指針和引用(int、int&、int&、int&*、int**), Pointers vs References in C++