c - 我的 fget 出现段错误?

标签 c segmentation-fault

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

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

#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函数中。因为这个我还无法测试我的代码的其他功能......

最佳答案

您已在 nrec 上调用了 realloc(),但从未使用 malloccalloc > 做了任何事情。

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;

关于c - 我的 fget 出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11784124/

相关文章:

c - 使用system()执行BASH脚本输出: source: not found

c - 为什么变量存储的值与用户输入的值不同?

linux - 如何在汇编中编写交换函数?

c++ - 无法弄清楚错误的来源(调试器说段错误)

c - strcat() int 变量 + C

C - 系统调用 - 64 位 - 指针

c - 为什么我的代码会给我垃圾值,有时还会出现段错误?

c - unix 下文件复制时出现段错误

c++ - 套接字上的并行读/写

渲染器和对象之间的 C++ SFML 段错误