洪翠憶
}
未初始化靜態變數
初始化靜態變數
唯讀區域(read-only area)
可讀寫區域(read-write area)
靜態變數
#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;
}
堆疊
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
stack
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
b
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
b
4
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
b
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
c
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
2
c
0
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
a
c
0
堆疊
#include <iostream>
int main(){
int a = 2;
if(a > 0){
int b;
b = 4;
}
int c;
return 0;
}
main()
stack
動態配置變數
#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;
}
a = 1, 輸出 = one
a = 2, 輸出 = one
two
a = 3, 輸出 = 3