看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) MacOS 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) GCC 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 在macOS一直出現segmentation fault(數字越來越大) 在win10結果錯誤1232367 在Linux正確無誤1245367 餵入的資料(Input): 預期的正確結果(Expected Output): 124532 錯誤結果(Wrong Output): [1] 2658 segmentation fault >./a.out [1] 2710 segmentation fault >./a.out 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> struct Node{ int data; struct Node *_child; struct Node *_child2; }; struct Node *creatTree(){ struct Node *_node = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node2 = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node3 = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node4 = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node5 = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node6 = (struct Node*)malloc(sizeof(struct Node)); struct Node *_node7 = (struct Node*)malloc(sizeof(struct Node)); _node -> data =1; _node2 -> data =2; _node3 -> data =3; _node4 -> data =4; _node5 -> data =5; _node6 -> data =6; _node7 -> data =7; _node -> _child =_node2; _node -> _child2=_node3; _node2 -> _child =_node4; _node2 -> _child2=_node5; _node3 -> _child =_node6; _node3 -> _child =_node6; _node3 -> _child2=_node7; return _node; } void printAll_DFS(struct Node* node){ if(node!=NULL){ printf("%d",node->data); if(node->_child!=NULL){ printAll_DFS(node -> _child); } if(node->_child2!=NULL){ printAll_DFS(node -> _child2); } } } int main(){ struct Node * tmp = NULL; tmp = creatTree(); printAll_DFS(tmp); printf("\n"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.114.123.157 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1498465781.A.A1C.html
ptt0720: 補充一下 這是在做完整二元樹的走訪 用DFS06/26 16:31
Hazukashiine: 我剛剛在linux上測:124536706/26 16:35
Hazukashiine: 你的 _node[4-7] 的 _child(2) 沒有 initialize06/26 16:37
Hazukashiine: 把 malloc 改成 calloc 試試06/26 16:37
※ 編輯: ptt0720 (140.114.123.150), 06/26/2017 16:56:38
ptt0720: 1245367無誤 我改正了 請問初始化哪邊呢 06/26 16:57
ptt0720: 初始化成null嗎 06/26 16:57
ptt0720: 成功了 非常感謝 請問windows系統沒報錯console也錯有人 06/26 17:02
ptt0720: 知道原因嗎 06/26 17:02
Hazukashiine: 初始化為 null 這個 scenario compiler 不太容易 06/26 17:29
Hazukashiine: 偵測出 "use uninitialize values" 所以要用其他的 06/26 17:29
Hazukashiine: 工具像是一些動態分析器 06/26 17:30
Hazukashiine: null -> NULL uninitialize -> ~d 06/26 17:31
Hazukashiine: 雖然我不確定用 calloc 取代 assign to NULL 有沒有 06/26 17:32
Hazukashiine: 符合語言標準 但是我的經驗這樣做基本上沒問題 06/26 17:33
Hazukashiine: https://stackoverflow.com/questions/29800636 06/26 17:35
MIKEmike07: Use Valgrind 測測看 06/26 17:49
hunandy14: 請問~是直接設0就好嗎 calloc(sizeof(Node), 0); 06/27 01:16
Bencrie: 讀手冊 06/27 09:26
hunandy14: 看一下,誤會用法了...已為像是 new T(value); 06/28 20:14
TobyH4cker: 寫一個createNode做申請跟初值化解決 07/03 04:39
kevin85421: struct中的pointer若是沒有初始化則address pointer 07/11 10:57
kevin85421: 中存的address會跑掉,所以我一直很佩服可以不用class 07/11 10:57
kevin85421: 來maintain一個大型的project的人 07/11 10:57
dou0228: struct 寫的好,是可以非常類似 class 的 07/12 09:41
dou0228: 樓上講的,看一下 Linux Kernel,只用 C 寫 07/12 09:42
dou0228: 那不叫做 pointer address 跑掉,要記得自行 initial 07/12 09:46