c - 释放嵌套结构的指针时出现段错误

标签 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);

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

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

非常感谢。

最佳答案

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

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);

关于c - 释放嵌套结构的指针时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242829/

相关文章:

c - 如何在 C 中为以下代码调试段错误。我在许多函数以及 main 中使用了 malloc 和 free

c++ - 隔离段错误 - 但为什么会发生?

c - gcc 优化给出无限循环或段错误

c - 如何读取Bios日期和时间?

c - 当函数返回指针时,为什么我得到 "initialization makes pointer from integer"?

c - 无法接收使用 MSG_MORE 标志发送的最后一个套接字数据

json - 阻止 gorm 在 where 子句中提示 Unknown column ‘users.deleted_at’

可以多次定义 C 结构吗?

c - 结构中 p* 的潜在问题?

arrays - 如何轻松访问结构体作为矩阵