[問題] segmentation fault產生原因

看板C_and_CPP作者 (濕濕)時間6年前 (2017/06/26 16:29), 6年前編輯推噓3(3023)
留言26則, 8人參與, 最新討論串1/1
開發平台(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

06/26 16:31, , 1F
補充一下 這是在做完整二元樹的走訪 用DFS
06/26 16:31, 1F

06/26 16:35, , 2F
我剛剛在linux上測:1245367
06/26 16:35, 2F

06/26 16:37, , 3F
你的 _node[4-7] 的 _child(2) 沒有 initialize
06/26 16:37, 3F

06/26 16:37, , 4F
把 malloc 改成 calloc 試試
06/26 16:37, 4F
※ 編輯: ptt0720 (140.114.123.150), 06/26/2017 16:56:38

06/26 16:57, , 5F
1245367無誤 我改正了 請問初始化哪邊呢
06/26 16:57, 5F

06/26 16:57, , 6F
初始化成null嗎
06/26 16:57, 6F

06/26 17:02, , 7F
成功了 非常感謝 請問windows系統沒報錯console也錯有人
06/26 17:02, 7F

06/26 17:02, , 8F
知道原因嗎
06/26 17:02, 8F

06/26 17:29, , 9F
初始化為 null 這個 scenario compiler 不太容易
06/26 17:29, 9F

06/26 17:29, , 10F
偵測出 "use uninitialize values" 所以要用其他的
06/26 17:29, 10F

06/26 17:30, , 11F
工具像是一些動態分析器
06/26 17:30, 11F

06/26 17:31, , 12F
null -> NULL uninitialize -> ~d
06/26 17:31, 12F

06/26 17:32, , 13F
雖然我不確定用 calloc 取代 assign to NULL 有沒有
06/26 17:32, 13F

06/26 17:33, , 14F
符合語言標準 但是我的經驗這樣做基本上沒問題
06/26 17:33, 14F

06/26 17:49, , 16F
Use Valgrind 測測看
06/26 17:49, 16F

06/27 01:16, , 17F
請問~是直接設0就好嗎 calloc(sizeof(Node), 0);
06/27 01:16, 17F

06/27 09:26, , 18F
讀手冊
06/27 09:26, 18F

06/28 20:14, , 19F
看一下,誤會用法了...已為像是 new T(value);
06/28 20:14, 19F

07/03 04:39, , 20F
寫一個createNode做申請跟初值化解決
07/03 04:39, 20F

07/11 10:57, , 21F
struct中的pointer若是沒有初始化則address pointer
07/11 10:57, 21F

07/11 10:57, , 22F
中存的address會跑掉,所以我一直很佩服可以不用class
07/11 10:57, 22F

07/11 10:57, , 23F
來maintain一個大型的project的人
07/11 10:57, 23F

07/12 09:41, , 24F
struct 寫的好,是可以非常類似 class 的
07/12 09:41, 24F

07/12 09:42, , 25F
樓上講的,看一下 Linux Kernel,只用 C 寫
07/12 09:42, 25F

07/12 09:46, , 26F
那不叫做 pointer address 跑掉,要記得自行 initial
07/12 09:46, 26F
文章代碼(AID): #1PKCNreS (C_and_CPP)