簡體   English   中英

當釋放嵌套結構的指針時出現分段錯誤

[英]When freeing a pointer for a nested struct getting Segmentation fault

這是我的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM