[問題] segmentation fault產生原因
開發平台(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
06/26 16:31, 1F
→
06/26 16:35, , 2F
06/26 16:35, 2F
→
06/26 16:37, , 3F
06/26 16:37, 3F
→
06/26 16:37, , 4F
06/26 16:37, 4F
※ 編輯: ptt0720 (140.114.123.150), 06/26/2017 16:56:38
→
06/26 16:57, , 5F
06/26 16:57, 5F
→
06/26 16:57, , 6F
06/26 16:57, 6F
→
06/26 17:02, , 7F
06/26 17:02, 7F
→
06/26 17:02, , 8F
06/26 17:02, 8F
→
06/26 17:29, , 9F
06/26 17:29, 9F
→
06/26 17:29, , 10F
06/26 17:29, 10F
→
06/26 17:30, , 11F
06/26 17:30, 11F
→
06/26 17:31, , 12F
06/26 17:31, 12F
→
06/26 17:32, , 13F
06/26 17:32, 13F
→
06/26 17:33, , 14F
06/26 17:33, 14F
→
06/26 17:35, , 15F
06/26 17:35, 15F
推
06/26 17:49, , 16F
06/26 17:49, 16F
→
06/27 01:16, , 17F
06/27 01:16, 17F
推
06/27 09:26, , 18F
06/27 09:26, 18F
→
06/28 20:14, , 19F
06/28 20:14, 19F
→
07/03 04:39, , 20F
07/03 04:39, 20F
推
07/11 10:57, , 21F
07/11 10:57, 21F
→
07/11 10:57, , 22F
07/11 10:57, 22F
→
07/11 10:57, , 23F
07/11 10:57, 23F
→
07/12 09:41, , 24F
07/12 09:41, 24F
→
07/12 09:42, , 25F
07/12 09:42, 25F
→
07/12 09:46, , 26F
07/12 09:46, 26F