Home > Software engineering >  1548132==ERROR: LeakSanitizer: detected memory leaks
1548132==ERROR: LeakSanitizer: detected memory leaks

Time:10-12

I ran the autograder on my program and got this error but when I run the exact same test case manually it seems to work fine. I am unsure why I get the error only when I use the autograder. I am new to C and I do not understand why the memory leak occurs because I have freed up all the variables that I used malloc on. It shows me what lines specifically cause the error but I do not know how to fix it without "ruining" my code/output.

The error message

The source code:

struct Node* head; //global variable

void Insert(int x){
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    struct Node* ptr = head;
    
    //check if already present
    while(ptr != NULL){
        if(ptr->data == x){return;}
        ptr = ptr->next;
    }
    
    //check if head is greater
    if(head == NULL || head->data > x){
        temp->data = x;
        temp->next = head;
        head = temp;  
    }else{
        ptr = head;
        while(ptr->next != NULL && ptr->next->data < x){
            ptr = ptr->next;
        }
        
        temp->data = x;
        temp->next = ptr->next;
        ptr->next = temp;
    }
   
}
void Delete(int x){
    struct Node* temp = head;
    struct Node* prev;
    
    //if head has key
    if(temp != NULL && temp->data == x){
        head = temp->next;
        free(temp);
        return;
    }
    
    //find key
    while(temp != NULL && temp->data != x){
        prev=temp;
        temp = temp->next;
    }
    
    //if key isnt present
    if(temp == NULL){return;}
    
    //unlink
    prev->next = temp->next;
    
    free(temp);
}
void Print(){
    struct Node* temp = head;
    int c = 0;
    while(temp != NULL){
        c  ;
        temp = temp->next;
    }
    printf("%d :", c);
    
    temp = head;
    while(temp != NULL){
        printf(" %d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    int x;
    char c;
    head = NULL; //empty list
    
    scanf(" %c%d",&c,&x);
    if(c == 'i'){
        Insert(x);
    }else if(c == 'd'){
        Delete(x);  
    }
    Print();
    while(scanf(" %c%d",&c,&x)!=EOF){
        if(c == 'i'){
            Insert(x);
        }else if(c == 'd'){
            Delete(x);
        } 
        Print();
    }
    
    return 0;
}

CodePudding user response:

Your problem is here

void Insert(int x){
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    struct Node* ptr = head;
    
    //check if already present
    while(ptr != NULL){
        if(ptr->data == x){return;}  <--- If this return is executed, temp is not freed
        ptr = ptr->next;
    }

To fix it just move the malloc so it's after the loop.

void Insert(int x){
    struct Node* ptr = head;
    
    //check if already present
    while(ptr != NULL){
        if(ptr->data == x){return;}
        ptr = ptr->next;
    }

    struct Node* temp = malloc(sizeof(struct Node));

BTW:

  1. Having head as a global variable is a bad idea

  2. Don't cast the value returned by malloc

  • Related