看板 C_and_CPP 關於我們 聯絡資訊
請問各位大大 我的程式在編譯的時候沒有問題 但是執行卻出現錯誤???WHY??? 有強者可以幫我找出問題嗎??? 我原本的struct如下 struct Node { int data; struct Node *left_child; struct Node *right_child; }; 後來我為了加入字串,所以改成 struct Node { int data; char name[10]; struct Node *left_child; struct Node *right_child; }; 然後修改了一下節點插入 但是就出現錯誤,奇怪的是編譯的時候 沒有問題,但是執行之後卻無法執行,出現下面訊離 ----------------------- Binday_tree2: malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. Aborted (core dumped) Press ENTER to continue...- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> struct Node { int data; char name[10]; struct Node *left_child; struct Node *right_child; }; typedef struct Node node; typedef node *bt; //-------------------------Search-----------------------------// bt search(bt ptr, int x) { bt temp; temp=ptr; if(ptr==NULL) return NULL; if(x< temp->data) return search(temp->left_child,x); else if(x> temp->data) return search(temp->right_child,x); return temp; } bt search_father(bt ptr, int X) { bt parent; parent = ptr; // 從ptr開始找 while(ptr != NULL) { if(ptr->data == X) // 找到目標 return parent; // 回傳此節點之父節點 else { parent=ptr; if(ptr->data>X) { // ptr在parent左子樹 ptr = ptr->left_child; // 往左找 } else { ptr=ptr->right_child; } } } return NULL; // 找不到 } //=============================================================// bt bst_insert(bt p, int x, char namex[]) { bt newnode; if(p==NULL) { newnode=(bt)malloc(sizeof(bt)); newnode->left_child=NULL; newnode->right_child=NULL; newnode->data=x; strcpy(newnode->name,namex); return newnode; } if(x<p->data) { p->left_child=bst_insert(p->left_child,x,p->name); } else if(x > p->data) { p->right_child=bst_insert(p->right_child,x,p->name); } else { } return p; } //-------------------------LDR--------------------------------// void inorderLDR(bt ptr) { if(ptr) { inorderLDR(ptr->left_child); printf("%d\n",ptr->data); inorderLDR(ptr->right_child); } } //--------------------------DLR-------------------------------// void inorderDLR(bt ptr) { if(ptr) { inorderDLR(ptr->left_child); printf("%c",ptr->data); inorderDLR(ptr->right_child); } } //---------------------------LRD------------------------------// void inorderLRD(bt ptr) { if(ptr) { inorderLRD(ptr->left_child); inorderLRD(ptr->right_child); printf(" %c ",ptr->data); } } //---------------------------------------------------------// bt GetNode() { bt NewNode; NewNode=(bt)malloc(sizeof(node)); if(NewNode==NULL) { printf("Memory is not enought\n"); exit(1); } return NewNode; } //-----------------------------------------------------------// void deleteNode(bt node_head) { int Bekilled; int status1=1; printf("Please Enter the number \n"); while((status1=scanf("%d",&Bekilled))!=1 || (Bekilled<1 || Bekilled>1000) ||(search(node_head,Bekilled)==NULL)) { if(status1!=1) scanf("%*s"); printf("Please Enter again\n"); } printf("\n\n\n"); bt father=search_father(node_head,Bekilled); bt ptrX=search(node_head,Bekilled); bt next; if((ptrX->left_child==NULL) && (ptrX->right_child!=NULL)) { if(father->data>ptrX->data) father->left_child=ptrX->right_child; else father->right_child=ptrX->right_child; free(ptrX); } else if((ptrX->right_child==NULL) && (ptrX->left_child!=NULL)) { if(father->data>ptrX->data) father->left_child=ptrX->left_child; else father->right_child=ptrX->left_child; free(ptrX); } else if((ptrX->right_child==NULL)&&(ptrX->left_child==NULL)) { if(father->data>ptrX->data) father->left_child=NULL; else father->right_child=NULL; free(ptrX); } else if((ptrX->right_child!=NULL)&&(ptrX->left_child!=NULL)) { father=ptrX; next=ptrX->left_child; // Ptrx=Want_delete while(next->right_child!=NULL) { father=next; next=next->right_child; } //printf("Next->value is %d\n", next->data); ptrX->data=next->data; //father->right_child=next->left_child; if(father!=ptrX) { father->right_child=next->left_child; } else { father->left_child=next->left_child; } free(next); } } //========================================================// int main(int argc, char **argv) { bt nodeX=NULL; bt node_head=NULL; char namex[20]; //bt Father_node; //bt Want_delete; //nodeX=bst_insert(nodeX, 30); //printf("-----------------------\n"); //printf("%d\n",nodeX->data); //printf("-----------------------\n"); //nodeX=bst_insert(nodeX, 10); //printf("-----------------------\n"); //printf("%d\n",nodeX->data); printf("-----------------------\n"); strcpy(namex,"AAA"); nodeX=bst_insert(nodeX, 50, namex); //node_head=nodeX; strcpy(namex,"BBB"); nodeX=bst_insert(nodeX, 40,namex); strcpy(namex,"CCC"); nodeX=bst_insert(nodeX, 60,namex); strcpy(namex,"DDD"); nodeX=bst_insert(nodeX, 30,namex); strcpy(namex,"EEE"); nodeX=bst_insert(nodeX, 45,namex); strcpy(namex,"DDD"); nodeX=bst_insert(nodeX, 55,namex); strcpy(namex,"CCC"); nodeX=bst_insert(nodeX, 70,namex); strcpy(namex,"EEE"); nodeX=bst_insert(nodeX, 65,namex); strcpy(namex,"FFF"); nodeX=bst_insert(nodeX, 54,namex); strcpy(namex,"GGG"); nodeX=bst_insert(nodeX, 57,namex); inorderLDR(nodeX); printf("\n\n"); // Father_node=search_father(node_head ,55); // printf("Father nnode is==%d\n", Father_node->data); // Want_delete=search(nodeX,55); //printf("searce_node_temp %d\n", searce_node_temp->data); deleteNode(node_head); inorderLDR(nodeX); //searce_node_temp=search(nodeX,65); //printf("searce_node_temp %d\n", searce_node_temp->data); //deleteNode(searce_node_temp); //inorderLDR(nodeX); //result=search(nodeX, 99); //if(result=1) // { // printf("Result is not in the list %d\n", result); // } // else // { // printf("Result got is %d\n" ,result); // } //deleteNode(N5); //inorderLDR(N1); printf("\n\n"); //inorderDLR(N1); //printf("\n"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.231.55.129 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1529212102.A.D69.html
cphe: 肉眼debug曠日費時,最快就是看你加了哪些東西才掛掉從那開 06/17 13:15
cphe: 始找,要不然debugger開下去就知道死在哪了 06/17 13:15
art1: 從錯誤訊息來看,似乎是記憶體配置錯誤,我猜是對齊問題? 06/17 14:10
art1: 修改 char[10] 裡面的 10 看看,看能不能改到一個可以運作的 06/17 14:11
outofyou: bst_insert裡面放到p->name, p 可能為null。 06/17 14:31
ilikekotomi: 用vs debugger發現你malloc的size是錯的 06/17 14:41
ilikekotomi: newnode=(bt)malloc(sizeof(bt)); 06/17 14:42
ilikekotomi: typedef node *bt; 這樣你sizeof(bt)回傳的會是 06/17 14:42
ilikekotomi: 指標的大小 應該要是sizeof(Node)的大小才對 06/17 14:43
ilikekotomi: 我只有看runtime錯掉的部分 其他要靠你自己了 06/17 14:50
ilikekotomi: 至少改過以後可以跑到enter number的地方 06/17 14:51
sarafciel: 把指標拿typedef藏掉星號不是好的coding style,建議改 06/17 15:45
sarafciel: 掉 06/17 15:45