如何解决用 C 编程时出错的原因 Segmentation fault (core dumped)
这是我的代码:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Alpha_Bet_Size 26
/*Binary tree - for dictionary implementation*/
typedef struct Word //Structure for the dictionary
{
struct Word *next[Alpha_Bet_Size]; //The alpha bet BTS pointers
char ch; //Char of the current node level
char *meaning;
}Word;
/*BTS FUNCTIONS*/
int toLower_W(char *word);
int CreateTree(Word **dictionary);
int Rec_Add_Word(char *word,char *meaning,Word **dic);
int Rec_Add_Word(char *word,Word **dic);
int Wrong_Word_System(Word **dic);
int Rec_Word_Print(char word,Word **dic);
int Print_meaning(char *word,Word **dic);
int Free_Dic();
int Free_Word();
int Add_Word(char *word,Word **dic);
//
int main(int argc,char *argv[])
{
Word *dic_system;
CreateTree(&dic_system);
char Name[10] = "REd";
char Meaning[50] ="A color";
Add_Word(Name,Meaning,&dic_system);
printf("Done\n");
return 0;
}
//
/*BTS*/
//Convert string to a lower case word
int toLower_W(char *word)
{
while(!word)
{
*word = tolower(*word);
word++;
}
return 0;
}
/*The function create a new tree*/
int CreateTree(Word **dictionary)
{
*(dictionary) = malloc(sizeof(Word));
if(!dictionary)
return -1; //MEmory allocation fails return -1;
return 0;
}
/*BTS Recursion in order to add a word*/
int Rec_Add_Word(char *word,Word **dic)
{
//Recursion Break point.
if(!(word+1)) //We assigned the whole word
{
(*dic)->ch = *word;
(*dic)->meaning = malloc(strlen(meaning)*sizeof(char));
if(!(*dic)->meaning)
return -1; //Memory allocation fails return -1.
strcpy((*dic)->meaning,meaning); //Allocating a new meaning to the word
return 0;
}
else
{
Word *letter = (*dic)->next[(*word-'a')];
if (letter) //There is already assigned letter
Rec_Add_Word(word+1,meaning,&letter);
letter = malloc(sizeof(Word)); //Allocating next node tree
if(!letter)//Allocation Failed
return -1;
letter->ch = *word;
Rec_Add_Word(word+1,&letter);
}
}
int Rec_Meaning_Print(char *word,Word **dic)
{
if(!(word+1)) //Last letter of the word
{
if(!(*dic)->meaning) //Found the meaning
{
printf("Meaning: %s",(*dic)->meaning);
return 0;
}
else
return 1; //We didn't find meaning - Auto correct spell system activation
}
else
Rec_Meaning_Print(word+1,&((*dic)->next[(*word)-'a']) ); //Recursion step
}
int Free_Dic()
{
return 0;
}
int Free_Word()
{
return 0;
}
/*The inputs are word,their meaning and a
pointer to the adress of the dictionary. The function adds a word.*/
int Add_Word(char *word,Word **dic)
{
toLower_W(word);
if(Rec_Add_Word(word,dic) == -1)//Memory allocation occured.
return -1;
return 0;
}
/*END*/
在编译过程中,我遇到了下一个错误:Segmentation fault (core dumped) . 我是 c 的中级程序员。为了丰富我的知识,我开始学习使用 linux 和 gcc 编译器。我用 gdb 调试代码并得到当前错误:
Program received signal SIGSEGV,Segmentation fault.
0x0000555555555400 in Rec_Add_Word ()
我试图通过较早的帖子来识别问题 - 这表明我试图访问未声明的内存,但我没有看到它。错误在哪里?背后的原因是什么,以及另一个一般性问题 - 是否有关于在 linux 中使用 gdb 进行编译的好指南? 最好的问候。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。