为什么这部分给出了分段错误?

时间:2014-07-30 18:36:38

标签: 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;
}

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

无论如何,我知道这是一个非常新手的代码,但我试图理解。

2 个答案:

答案 0 :(得分:2)

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

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

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

   currfile->next = NULL;

答案 1 :(得分:0)

您在哪里初始化files? (提示:你没有)。这意味着它是一个未定义的值(不一定是NULL)。您需要在致电files之前初始化putFile

正如评论中所述,一旦您修复了该问题,您需要对nextFileCoordinates中的FileStruct以及coordinates中的FileStruct执行相同操作1}}。

同样如评论中所述,您重用了word缓冲区,这意味着树中的所有节点都将具有相同的字符串。当字符串存储在树中时,应为字符串分配新的缓冲区。也许使用strdup

(*leaf)->word = strdup(word);

修复后,您还需要处理字符串比较。 if(word == (*leaf)->word)比较指针,而不是它们的内容。如果要比较实际的字符串数据,则需要使用strcmp

else if(strcmp(word, (*leaf)->word) == 0){