Error - double free or corruption (fasttop) in C

794 views Asked by At

I am trying to write a program using header files to process lists and files for an assignment, but I'm having trouble with the error mentioned in the title. The error happens in the 3rd time addNode runs (addNode(l, r3);), and the program crashes. I've been trying to find out what might be causing it for almost 2 hours now, and have come up with nothing.

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
#include "list.h"

int main(){
    int i;
    list l;
    l = initList();
    node r1, r2, r3, r4, curNode;
    r1 = malloc(sizeof(struct nodeR));
    r2 = malloc(sizeof(struct nodeR));
    r3 = malloc(sizeof(struct nodeR));
    r4 = malloc(sizeof(struct nodeR));

    strcpy(r1->payload, "test1");
    strcpy(r2->payload, "test2");
    strcpy(r3->payload, "test3");
    strcpy(r4->payload, "test4");

    addNode(l, r1);
    addNode(l, r2);
    addNode(l, r3);
    addNode(l, r4);

    curNode = l->head;
    for (i = 1; i < l->length; i++){
        printf("%s\n", curNode->payload);
        curNode = curNode->next;
    }

    reverseList(l);

    curNode = l->head;
    for (i = 1; i < l->length; i++){
        printf("%s\n", curNode->payload);
        curNode = curNode->next;
    }

}

list.h:

#ifndef LIST_H_
#define LIST_H_

#define TRUE 1
#define FALSE 0
typedef struct nodeR* node;
struct nodeR{
    char payload[20];
    node next;
    node previous;
};
typedef struct listR* list;
struct listR{
    node head;
    node tail;
    int length;
};
list initList(); //creates a new list and returns it. Returns NULL if it fails
int destroyList(list l); //frees memory from the list. Returns TRUE upon success or FALSE upon failure
int getNodeIndex(list l, node targetNode); //returns the position of the targetNode in the list. Returns a negative number if it fails
int getListLength(list l); //returns the length of the list. Returns a negative number if it fails
int addNode(list l, node newNode); //adds newNode at the end of the list. Returns TRUE upon success or FALSE upon failure
int insertNodeBefore(list l, node targetNode, node newNode); //injects newNode in the list right before the targetNode. Returns TRUE upon success or FALSE upon failure
int deleteNode(list l, node targetNode); //deletes targetNode from list. Returns TRUE upon success or FALSE upon failure
list reverseList(list l); //returns a list which is created by reversing the order of the elements of l. Returns NULL if it fails

#endif

addNode in list.c

int addNode(list l, node newNode){
list temp;
if (l == NULL){     
    printf("Error: List does not exist or has already been destroyed.");
    return FALSE;
}

if (getListLength(l) == 0){ 
    l->head = newNode;
    l->tail = newNode;
    l->length++;
}
else {              
    l->tail->next = newNode;
    newNode->previous = l->tail;
    l->tail = newNode;
    l->length++;
}
return TRUE;

}

Note that: a) The header file was given by the professor, the assignment is to code list.c b) The actual code is quite large, and as far as I know irrelevant to the problem, so I chose to only post the problematic function. c) main.c is just a test program, aimed at testing if my code is functional. That said, it was written by me, not the professor, so the problem might be in that. d) As I said above, the crash happens during the 3rd time addNode runs, for the node r3.

Thanks in advance for your help.

0

There are 0 answers