c - 为什么这部分会出现段错误?

标签 c pointers segmentation-fault

这是一个不完整的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W 1031
#define B 256

struct FileCoordinates{
    int x;   /*line number*/
    int y;   /*word number*/
    struct FileCoordinates *next;
};

struct FileStruct{
    char *filename;
    struct FileCoordinates *coordinates;
    struct FileStruct *next;
};

struct WordStruct{
    char *word;
    struct WordStruct *left;
    struct WordStruct *right;
    struct FileStruct *files;
};

typedef struct FileCoordinates *CoorPtr;
typedef struct FileStruct *FilePtr;
typedef struct WordStruct *WordPtr;

WordPtr HashTable[W];

long int power(int a, long b){
    long int pow, i;
    pow = 1;
    for (i = 0; i < b; i++){
        pow = pow*a;
    }
            return pow;
}

int hashvalue (char *word){
    long int i, value=0, n;
    n = strlen(word);
    for (i=0; i<n; i++){
        value = value + power(B,n-i-1) * word[i];
    }
    return(value%W);
}

void putPosition(int x, int y, FilePtr *currfile){
    CoorPtr currcors = (*currfile)->coordinates;
    while (currcors!=NULL){
        currcors = currcors->next;
    }
    currcors = (CoorPtr)malloc(sizeof(struct FileCoordinates));
    currcors->x=x;
    currcors->y=y;
}
void putFile(char *filename, WordPtr *currWord, int x, int y){
    FilePtr currfile = (*currWord)->files;
    while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
        currfile=currfile->next;
    }
    if (strcmp(currfile->filename,filename)==0){
        putPosition(x, y, &currfile);
    }
    else{
        currfile = (FilePtr)malloc(sizeof(struct FileStruct));
       currfile->filename = filename;
       putPosition(x, y, &currfile);
    }
}

void insert(char *word, WordPtr *leaf, char *filename, int x, int y)
{
    if( *leaf == NULL )
    {
        *leaf = (WordPtr) malloc( sizeof( struct WordStruct ) );
        (*leaf)->word = word;
        putFile(filename, &(*leaf), x, y);
        /* initialize the children to null */
        (*leaf)->left = 0;
        (*leaf)->right = 0;
    }
    else if(word < (*leaf)->word)
    {
        insert( word, &(*leaf)->left, filename, x, y);
    }
    else if(word > (*leaf)->word)
    {
        insert( word, &(*leaf)->right, filename, x, y);
    }
    else if(word == (*leaf)->word){
        putFile(filename, &(*leaf), x, y);
    }
}

int main(int argc, char *argv[]){
    int i, words, lines, value;
    char *filename, *word, c;
    FILE *fp;
    word = (char *)malloc(21*sizeof(char));
    if (argc<2){
        perror("no files were inserted");
    }
    for (i=1; i<argc; i++){
        words=1;
        lines=1;
        fp = fopen(argv[i], "r");
        if (fp==NULL){
             printf("Could not open file named %s! \n", argv[i]);
             return 2;
            }
        filename = malloc( strlen( argv[i] ) + 1 );
        strcpy( filename, argv[i] );
        fscanf(fp, "%s", word);
        value=hashvalue(word);
        c=getc(fp);
        insert(word, &HashTable[value], filename, lines, words);
        if (c==' '){
            words = words+1;
        }
        else if(c=='\n'){
            lines=lines+1;
            words=1;
        }


    }
    system("PAUSE");
    return 0;
}

调试器在这部分给了我段错误:

while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
    currfile=currfile->next;
}

代码的原因是获取文本文件作为参数,将单词排序到哈希表中的二叉树中,然后通过搜索关键字显示它出现的坐标。

无论如何,我知道这是一个非常新手的代码,但我正在努力理解。

最佳答案

创建对象时忘记将currfile->next设置为NULL:

   currfile = (FilePtr)malloc(sizeof(struct FileStruct));
   currfile->filename = filename;

calloc 代替 malloc 预留空间,或者添加:

   currfile->next = NULL;

关于c - 为什么这部分会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25044423/

相关文章:

c - 如何在 Visual Studio (Mac) 中运行 C 程序

c - 我的代码返回了一个意外的值

c - "12345"+ 2 在 C 中做什么?

c++ - 从函数返回变量地址时如何修复 'address of stack memory associated with local variable'?

将一个结构的属性复制到另一个相同类型的结构

c - 使用指针数组时循环内存地址

c - 如何释放结构体c中的双指针?

c - 段错误 :core Dumped while using mvscanw function of ncurses in c on ubuntu

C++:ToUnicode 函数上的 SIGSEGV (windows)

c - 用于读取数据库的损坏的 C 代码