释放嵌套结构的指针时会出现Segmentation fault

时间:2015-03-24 21:03:11

标签: c struct segmentation-fault free

这是我的代码:

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

#define NAMESIZE 20
#define LINESIZE 1024

typedef struct name name;
struct name
{
    char last[NAMESIZE];    /* last name */
    char first[NAMESIZE];   /* first name*/
};

typedef struct record record;
struct record
{
    name name;
    int score;
};

typedef struct record_list record_list;
struct record_list
{
    record *data;   /* the dynamic array of records */
    size_t nalloc;  /* number of records allocated */
    size_t nused;   /* number of records in use */
};

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

int list_insert(record_list *list, const record *rec)
{   
    size_t newSize;
    record *tmp;

    if(list -> nalloc == list -> nused)
    {
        if(list -> nalloc == 0)
        {
            newSize = 1;
        }
        else
        {
            newSize = 2 * list -> nalloc;
        }

        tmp = realloc(list -> data, newSize * sizeof(record));

        if(tmp == 0)
        {
            return 0;
        }

        list -> data = tmp;
        list -> nalloc = newSize;
    }

    list -> data[list -> nused++] = *rec;

    return 1;
}

void list_destroy(record_list *list)
{
    printf("Attempting Deletion");
    free(list->data);
    free(list->nalloc);
    free(list->nused);

    list -> data = 0;
    list -> nalloc = 0;
    list -> nused = 0;
}

int main(void){
    record_list list;
    record *r;
    name n;

    int score;

    char input[NAMESIZE];
    char name[NAMESIZE];
    char lname[NAMESIZE];


    list_init(&list);
    while(input != NULL) {
        printf("Please enter a value for Name: ");
        scanf("%s", input);
        strcpy(input, name);
        printf("Enter last name: ");
        scanf("%s", input);
        strcpy(input, lname);
        printf("Enter score: ");
        scanf("%d", &score);

        r=(record*)malloc(sizeof(record));
        if(r == NULL){
            printf("There isn't enough memory.\n");
        }
         strcpy(n.first, name);
         strcpy(n.last, lname);
         r -> name = n;
         list_insert(&list, r);
         printf("\n");
         printf("Choose next action:\n");
         printf("\tTo add more type \"add\";\n");
         printf("\tTo delete all records type \"del\";\n");
         scanf("%s", input);
         if(strcmp(input, "del") == 0){
            list_destroy(&list);
            printf("Deleted");
            break;
        }
    }


    return 1;
}

我正在进行一项小型实验练习,我们制作一个结构,填充它并在用户需要时清除它。昨天一切正常,但今天我似乎要么没有保存或破坏了一些东西,因为我收到了大量的错误。 这是我得到的错误的一个例子:

基本上当我调用方法时

void list_destroy(record_list *list);

它在到达第一个print语句之前崩溃,这意味着我对方法调用做错了。

总结的问题:什么可能导致分段错误(我在哪里访问不正确的内存)或者如何在不使用free的情况下清除结构内存?

非常感谢。

1 个答案:

答案 0 :(得分:2)

这应该说明你的问题是什么:

code.c: In function 'list_destroy':
code.c:74: warning: passing argument 1 of 'free' makes pointer from integer without a cast
code.c:75: warning: passing argument 1 of 'free' makes pointer from integer without a cast

你正试图释放int字段。你不能释放它们,因为它们不是指向内存块的指针。

因此,删除以下代码行:

free(list->nalloc);
free(list->nused);