分割错误核心转储 [英] Segmentation fault & core dump

查看:98
本文介绍了分割错误核心转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试自己编写二进制树。在我想扫描要添加的新字符串之前,它似乎一直有效。相同的字符串有效,新的字符串给我分割错误&核心转储。我怀疑分配新元素的内存有问题。

I've been trying to code up binary tree by my own. It seems to be working until I want to scanf for new string to add. Same string works, new string gives me segmentation fault & core dump. I suspect there's something wrong with mallocing memory of new element.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//------- wezel -------//
struct node{
char *word;
unsigned int arity;
struct node *left, *right, *parent;
};
struct node *root;
/* Adding new string to tree */
void dopisz(char wordtmp[50], struct node *start){

    if(root==NULL){ // empty tree, add as root
    root=(struct node*)malloc(sizeof *root);
    root->word=(char*)malloc(sizeof wordtmp);
    root->word=strcpy(root->word, wordtmp);
    root->arity=1;
    root->left=NULL;
    root->right=NULL;
    root->parent=NULL;
    }
    else if(strcmp(wordtmp, start->word)==0){
        start->arity=start->arity+1;
    }
    else if(strcmp(wordtmp, start->word)<0){  //if the added element is <
        if(start->left==NULL){ //if there's no left son
            struct node *nowy=(struct node*)malloc(sizeof *root);
            nowy->word=strcpy(nowy->word, wordtmp);
            nowy->arity=1;
            nowy->left=NULL;
            nowy->right=NULL;
            nowy->parent=start;
            start->left=nowy;
        }
        else if(start->left!=NULL){ //if there's left son
            dopisz(wordtmp, start->left);
        }

    }
    else if(strcmp(wordtmp, start->word)>0){  //if the added element is >
        if(start->right==NULL){ //if there's no right son
            struct node *nowy=(struct node*)malloc(sizeof *root);
            nowy->word=strcpy(nowy->word, wordtmp);
            nowy->arity=1;
            nowy->left=NULL;
            nowy->right=NULL;
            nowy->parent=start;
            start->right=nowy;
        }
        else if(start->right!=NULL){ //if there's right son
            dopisz(wordtmp, start->right);
        }

    }


}
//-------looking for minimum -------//
struct node* least(struct node *start){
    if(start->left != NULL){
        return least(start->left);
    }
    else return start;
}

//------- deleting -------//
void usun(){

}
//------- printing -------//
void drukuj(struct node *start){ //printing in order in order
    if(start->left!=NULL){
        drukuj(start->left); 
    }
    printf("%s (%d)\n", start->word, start->arity);
    if(start->right!=NULL){
        drukuj(start->right);
    }
}
//------- main -------//
int main(){
char wordtmp[50];
printf("\t Drzewo Poszukiwan Binarnych \n------------------------\n\n");
int x, y=0;
while(y==0){
    printf("\n MENU: \n 0 -> zakoncz \n 1 -> dopisz\n 2 -> usun\n 3 -> drukuj\n\n"); // 0 - exit, 1 - add, 2 - delete, 3 - print
    scanf("%d", &x);
    switch(x){
    case 0: y++;        break;
    case 1:
    printf("wpisz slowo: ");
    scanf("%s", wordtmp);
    dopisz(wordtmp, root);
    break;
    case 2: usun();     break;
    case 3: drukuj(root);   break;
    }
}
return 0;
}


推荐答案

直线

nowy->word=strcpy(nowy->word, wordtmp);

是错误的。 nowy->单词没有指向任意内存的任何存储点。将字符串复制到该字符串具有不确定的结果,但可能会出现段错误。

is wrong. nowy->word doesn't have any storage points to arbitrary memory. Copying a string to it has undefined results but a seg fault is likely.

您可以通过制作 word 来解决此问题。在节点的定义中固定大小的数组,或者通过动态为其分配内存

You can fix this by making word a fixed size array in node's definition or by allocating memory for it dynamically

nowy->word=malloc(strlen(wordtmp)+1);
strcpy(nowy->word, wordtmp);

nowy->word=strdup(wordtmp); // not standard C but available in Posix systems

这篇关于分割错误核心转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆