我的fgets分段错误?

时间:2012-08-02 19:05:06

标签: c segmentation-fault

似乎我的程序有一些导致错误的东西,并且在尝试调用fgets后它被卡住了。我可能是错的,但这似乎停止了,所以任何人都知道问题是什么?

该文件包含我的标题,其中包含定义和结构,主要用于对已完成的列表进行排序。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include"compare.h"
#define BLOCK 2

int input_record(record *rec, record_list *list);
void list_init(record_list *list);
void list_destroy(record_list *list);
void list_print(record_list *list);
int check_args(const char arg1[], const char arg2[], record_list *list);

int main(int argc, char *argv[]){
record_list list;
record rec;
char arg1[3];
char arg2[3];

/*check for proper number of arguments*/
if(argc > 3 || argc < 1){
    return 0;
}
/*run default if no switches*/
if(argc == 1){
    input_record(&rec, &list);
} else {

    strcpy(arg1, argv[1]);
    strcpy(arg2, argv[2]);
    arg1[3] = '\0';
    arg2[3] = '\0';

    input_record(&rec, &list);
    if(check_args(arg1, arg2, &list) == 0){
        printf("error");
    }
}

list_print(&list);
list_destroy(&list);

return 0;
}

int input_record(record *rec, record_list *list){
    int score;
    int min = -1;
    int max = 101;
    int count = 0;
    char first_name[NAMESIZE];
    char last_name[NAMESIZE];
    char temp[LINESIZE];
    record *nrec;

    list_init(list);

    for(count = 0; count < NLINES; count++){
        printf("Enter first name, last name, score\n");
/* !! seg fault occurs once i enter the data !! */
        if(fgets(temp, LINESIZE, stdin) == 0){
            break;
        }
        printf("222222");
        if(sscanf(temp, "%s %s %d", first_name, last_name, &score) == 3){
            if(strlen(first_name) >= NAMESIZE || strlen(last_name) >= NAMESIZE){
                printf("error");
                continue;
            }              
            if(score < min || score > max){
                printf("error");
                continue;
            }
        }

record *tempRec;

        if(list->nalloc == 0){
            tempRec = malloc(sizeof(record));
            if(tempRec == 0){
                return 0;
            }
            #ifdef DEBUG
                fprintf(stderr, "#\n");
            #endif
            list->nalloc = 1;
            list->data = tempRec;

            }else if(list->nalloc == list->nused){
                tempRec = realloc(list->data, (list->nalloc*BLOCK) * sizeof(record));
                if(tempRec == 0){
                    return 0;
                }
            list->data = tempRec;
            list->nalloc *= BLOCK;  
        }

        list->data[list->nused] = *rec;
        strcpy(rec->name.first, first_name);
        strcpy(rec->name.last, last_name);
        rec->score = score;
        list->nused++;    
    }
return 1;
}

void list_init(record_list *list){
list->nused = 0;
list->nalloc = 0;
list->data = 0;
}

void list_destroy(record_list *list){
free(list->data);
list->nalloc = 0;
list->nused = 0;
}

void list_print(record_list *list){
int count;
int listsize = sizeof(list);

for (count = 0; count < listsize; count++){
    printf("%s %s %d", list->data[count].name.last, list->data[count].name.first, list->data[count].score);
}
}

int check_args(const char arg1[], const char arg2[], record_list *list){
char nameDown[] = "-n";
char nameUp[] = "+n";
char scoreDown[] = "-s";
char scoreUp[] = "+s";

/*if either argument is not one of the valid switches, kill function*/
if((strcmp(arg1, nameDown) != 0) || (strcmp(arg1, nameUp) != 0) || (strcmp(arg1, scoreDown) != 0) || (strcmp(arg1, scoreUp) != 0)){
    return 0;
}

if((strcmp(arg2, nameDown) != 0) || (strcmp(arg2, nameUp) != 0) || (strcmp(arg2, scoreDown) != 0) || (strcmp(arg2, scoreUp) != 0)){
    return 0;
}

/*if the arguments are the same, kill function*/
if (strcmp(arg1, arg2) == 0){
        return 0;
/*otherwise look for all combinations of valid switches*/
} else if (strcmp(arg1, nameDown) == 0){
    if (strcmp(arg2, scoreDown) == 0){
        sort_desc_name_desc_score(list->data);
    } else if (strcmp(arg2, scoreUp) == 0){
        sort_desc_name_asc_score(list->data);
    /*if same lettered switches are detected, kill function*/
    } else if (strcmp(arg2, nameUp) == 0){
        return 0;
    } else {
        /*the ps and qs are supposed to be records to be sorted*/
        sort_desc_name(list->data);
    }
} else if (strcmp(arg1, nameUp) == 0){
    if (strcmp(arg2, scoreDown) == 0){
        sort_asc_name_desc_score(list->data);
    } else if (strcmp(arg2, scoreUp) == 0){
        sort_asc_name_asc_score(list->data);
    } else if (strcmp(arg2, nameDown) == 0){
        return 0;
    } else {
        sort_asc_name(list->data);
    }

} else if (strcmp(arg1, scoreDown) == 0){
    if (strcmp(arg2, nameDown) == 0){
        sort_desc_score_desc_name(list->data);
    } else if (strcmp(arg2, nameUp) == 0){
        sort_desc_score_asc_name(list->data);
    } else if (strcmp(arg2, scoreUp) == 0){
        return 0;
    } else {
        sort_desc_score(list->data);
    }

} else if (strcmp(arg1, scoreUp) == 0){
    if (strcmp(arg2, nameDown) == 0){
        sort_asc_score_desc_name(list->data);
    } else if (strcmp(arg2, nameUp) == 0){
        sort_asc_score_asc_name(list->data);
    } else if (strcmp(arg2, scoreDown) == 0){
        return 0;
    } else {
        sort_asc_score(list->data);
    }
/*and in case anything else got through*/
} else {
    return 0;
}

return 1;
}

即整个程序,在input_record函数中发生seg错误。我还没有能够测试其他功能的代码......

1 个答案:

答案 0 :(得分:0)

您已在realloc()上致电nrec,但您从未malloc'或calloc为此提供任何内容。

nrec = realloc(list->data, (list->nalloc + 1) * sizeof(record));

您有list的引用,但您也没有与之关联的内存。因此,一旦您尝试通过realloc()访问它,就会出现段错误。

如果没有分配任何内容,由于可能的内存泄漏,您永远不应该直接将任何内存分配的结果存储到引用中。更安全的解决方案如下:

temp = malloc(SIZE OF DATA);
if(temp == NULL)
{
    priontf("Malloc returned null!\n");
    return -1;
}
nrec = temp;