Memory Concept in C/C++
洪翠憶
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9028485/aqua.jpg)
Where Are My Variables Go?
究竟我們宣告的那些東東,都被電腦存放到哪裡了呢?
究竟我們宣告的那些東東,都被電腦存放到哪裡了呢?
究竟我們宣告的那些東東,都被電腦存放到哪裡了呢?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032259/cardboard-box-removebg-preview.png)
究竟我們宣告的那些東東,都被電腦存放到哪裡了呢?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032259/cardboard-box-removebg-preview.png)
究竟我們宣告的那些東東,都被電腦存放到哪裡了呢?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032259/cardboard-box-removebg-preview.png)
?
究竟我們宣告的那些東東,都被電腦存放到哪裡了呢?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032259/cardboard-box-removebg-preview.png)
?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032947/memory.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032947/memory.png)
}
靜態變數區段
未初始化靜態變數
初始化靜態變數
這裡會儲存:
-
全域變數(Global Variable)
-
靜態變數(Static Variable)
唯讀區域(read-only area)
可讀寫區域(read-write area)
Static Variable
靜態變數
-
與程式擁有相同生命週期的變數
-
在程式執行前就被系統靜態分配儲存空間
-
全域變數
-
以𝐬𝐭𝐚𝐭𝐢𝐜宣告
Example
#include <iostream>
int a = 0; //全域變數 & 有初始值 & 可讀寫,存於data的可讀寫區域
int arr[3] = {1, 3, 5}; //同上
const int b = 0; //全域變數 & 有初始值 & 唯讀,存於data的唯讀區域
int c; //全域變數 & 無初始值,存於bss
int main(){
static int d; //靜態變數 & 無初始值,存於bss
int e; //區域變數
return 0;
}
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032947/memory.png)
堆疊區段
這裡會儲存:
-
區域變數(Local Variable)
-
呼叫函數需要儲存的資訊
-
遞迴依靠這種儲存方式運作
-
由高往低疊(與heap的方向相反)
-
編譯器自動分配
-
記憶體空間是連續的
-
超出編列空間會溢位(overflow)
Stack
堆疊
-
先進後出
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
stack
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
b
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
b
4
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
b
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
c
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
c
0
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
c
0
Stack
堆疊
-
先進後出
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032947/memory.png)
heap(堆)區段
這裡會儲存:
-
動態配置變數(Dynamic Variable)
-
由低往高成長(與stack方向相反)
-
程式設計師自行分配記憶體(若未好好釋放記憶體,可能會造成記憶體洩漏)
-
記憶體空間不連續
DYNAMIC VARIABLE
動態配置變數
-
𝐧𝐞𝐰/𝐝𝐞𝐥𝐞𝐭𝐞(C++)或𝐦𝐚𝐥𝐥𝐨𝐜/𝐟𝐫𝐞𝐞(C)建立的變數
-
𝐧𝐞𝐰/𝐝𝐞𝐥𝐞𝐭𝐞詳細用法下下下下次上課會教( ̄∇ ̄)
#include <iostream>
int main(){
int *a = new int;
*a = 10;
std::cout << *a << '\n';
delete a;
int *b = (int*) malloc(sizeof(int));
*b = 20;
std::cout << *b;
free(b);
return 0;
}
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032947/memory.png)
文字區段
-
存放可執行的CPU指令
-
於stack和heap之後
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9032947/memory.png)
系統區段
-
儲存一些命令列參數與環境變數(與系統有關)
Kahoot!
Q1-4
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9038499/pasted-from-clipboard.png)
a = 1, 輸出 = one
a = 2, 輸出 = one
two
a = 3, 輸出 = 3
Q5
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9038503/pasted-from-clipboard.png)
Q6-7
![](https://s3.amazonaws.com/media-p.slid.es/uploads/1817947/images/9038509/pasted-from-clipboard.png)
Reference
C/C++ Memory
By justhentai
C/C++ Memory
- 443