Home > database >  malloc(): corrupted top size when adding to linked list
malloc(): corrupted top size when adding to linked list

Time:04-20

I wrote a code that adds a reservation to a linked list. When I add the first reservation everything works and the program prints the list correctly. But when I call the function a second time it gives malloc():corrupted top size error. I use printf to try and see where the code reaches but when I call the function a second time, it never reaches "passes malloc". Could it be my insertEnd function? Here's the code I wrote:

int main() {
 node *begin = NULL;
 node *end = NULL;

 char command;
 scanf("%c",&command);

 while(c != 'x') {

   if(c == 'n') {
     addReservations(begin,end);
   }
  
   scanf("%c",&command);
 }
 
 return 0;
}

#define RES 65000
typedef struct reservation {
  char *code;
  int numberClients;
} Reservation;

void addReservations(node *list_reservations, node *listend){
    char *code;
    int numberClients;
    Reservation new_reservation;

    char reservation[RES];
    char *nClientSTR;

    scanf("%[^\n]", reservation);

    code = strtok(reservation, " ");
    nClientSTR = strtok(NULL, " ");

    sscanf(nClientSTR, "%d",  numberClients);

    printf("tries malloc\n");
    new_reservation.code = (char *) malloc((strlen(code) 1*sizeof(char));
    printf("passes maloc\n");

    strcpy(new_reservation.code, code);
    new_reservation.numberClients = numberClients;
    insertEnd(new_reservation, &list_reservations, &listend);
    printList(&list_reservations);
}

//EDIT: I forgot my insertEnd function. Sorry!

typedef struct node {
  Reservation value;
  struct node* next;
  struct node* prev;
} node;

void insertEnd(Reservation el, node **begin, node **end) {
    /*cria um no para a lista*/
    node *node = malloc(sizeof(node)*1);
    node->value = el;
    node->prev = NULL;
    node->next = NULL;

    if(*begin== NULL) {
        *begin = node;
        *end = node;
        return;
    }

   else {
       
       (*end)->next = node;
       node->prev = *end;
       *end = node;
   }
}

Thank you in advance!

CodePudding user response:

There were a lot of issues with the user input in regards to the reservation initialization. I didn't even attempt to fix those but this should fix your error with malloc.

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

#define RES 65000
typedef struct reservation {
    char *code;
    int numberClients;
} Reservation;

typedef struct node {
    Reservation value;
    struct node *next, *prev;
} node;

// node *before may be null, but node *after may not be
void insert_after(node *before, node *after) {
    after->prev = before;
    if (before) {
        after->next = before->next;
        if (before->next) {
            before->next->prev = after;
        }
        before->next = after;
    } else {
        after->next = NULL;
    }
}

// Note: prints a linked list backwards
void print_list(node *head) {
    while (head) {
        printf("%s\n", head->value.code);
        head = head->prev;
    }
}

void add_reservations(node **list_end) {
    node *new_node = malloc(sizeof(node));
    if (!new_node) {
        printf("malloc failed\n");
        exit(1);
    }
    
    // Add whatever user input needed to set up new_node->value here...
    // For now, a pre-set string is given to make sure print_list doesn't try to print garbage
    new_node->value.code = "temp";
    new_node->value.numberClients = 10;
    
    insert_after(*list_end, new_node);
    *list_end = new_node; // doing this is fine but changing list_end is probably better left up to the caller so its change seems less 'magical'
}

int main() {
    node *list_end = NULL;
    
    char command;
    scanf("%c", &command);
    while(command != 'x') {
        if(command == 'n') {
            add_reservations(&list_end);
            print_list(list_end);
        }
        scanf("%c", &command);
    }
    
    while (list_end) {
        node *to_free = list_end;
        list_end = list_end->prev;
        free(to_free);
    }
    return 0;
}
  • Related