從愚蠢的錯誤學習
記錄一下今天犯的愚蠢錯誤,先來看看錯誤的程式碼:
輸入檔內容:
3
4 4 4
5 5 5
1 2 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| #include int main(int argc, constchar*argv[]){ cin>> n; |
3
1 2 3
1 2 3
1 2 3
不過最奇妙的是,這個是不會產生 segmentation fault 的!而且還可以順利列印出數值(雖然是錯的),於是我加了以下程式列印出記憶體位址與大小:
printf("weight addr: %x\n", &weight[1]);
printf("weight size: %u\n", sizeof(weight));
printf("dis addr: %x\n", &dis);
printf("dis size: %u\n", sizeof(dis));
printf("parent addr: %x\n", &parent);
printf("parent size: %u\n", sizeof(parent));
printf("visited addr: %x\n", &visited);
printf("visited size: %u\n", sizeof(visited));
weight addr: bfffe6b0
weight size: 0
dis addr: bfffe6a0
dis size: 0
parent addr: bfffe690
parent size: 0
visited addr: bfffe680
visited size: 0