TheUnknown
TheUnknown

Reputation: 53

When freeing a pointer for a nested struct getting Segmentation fault

This is my code:

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

I am working on a small lab exercise where we make a struct, fill it and clear it if the user needs to. Yesterday everything worked but today I seem to either have not saved it or broke something because I am getting a ton of errors. Here is an example of the error I'm getting:

Essentially when I call a method

void list_destroy(record_list *list);

it crashes before reaching the first print statement which means I am doing something wrong with the method call.

Summarized question: What could be causing the segmentation fault (where am I accessing incorrect memory) Or how else can I clear my struct memory without using free?

Thank you very much.

Upvotes: 0

Views: 252

Answers (1)

juhist
juhist

Reputation: 4314

This should tell what your problem is:

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

You're trying to free int fields. You can't free them because they are not pointers to memory blocks.

So, remove these lines of code:

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

Upvotes: 2

Related Questions