Home > Mobile >  ERROR: LeakSanitizer: detected memory leaks
ERROR: LeakSanitizer: detected memory leaks

Time:10-12

I keep getting the following error when I run my program: error message

It mentions leaks in lines 15, 64, and 110. I think the error is in the push function but I do not see where I could free the variable without causing errors in my code.

Line 15: struct sNode* new_node = (struct sNode*)malloc(sizeof(struct sNode));

Line 64: push(&stack, exp[i]);

Line 110: int n = areBracketsBalanced(argv[1]);

This is my code:

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

// structure of a stack node
struct sNode {
    char data;
    struct sNode* next;
};

void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
void push(struct sNode** top_ref, int new_data){
    struct sNode* new_node = (struct sNode*)malloc(sizeof(struct sNode));

    if (new_node == NULL) {
        printf("Stack overflow \n");
        getchar();
        exit(0);
    }

    new_node->data = new_data;
    new_node->next = (*top_ref);
    (*top_ref) = new_node;
}
int pop(struct sNode** top_ref){
    char res;
    struct sNode* top;
    
    if (*top_ref == NULL) {
        printf("Stack overflow \n");
        getchar();
        exit(0);
    }else {
        top = *top_ref;
        res = top->data;
        *top_ref = top->next;
        free(top);
        return res;
    }
}

// Returns 1 if character1 and character2 are matching left and right Brackets
bool isMatchingPair(char character1, char character2)
{
    if (character1 == '(' && character2 == ')')
        return 1;
    else if (character1 == '{' && character2 == '}')
        return 1;
    else if (character1 == '[' && character2 == ']')
        return 1;
    else
        return 0;
}

// Return 1 if expression is balanced
int areBracketsBalanced(char exp[]){
    int i = 0;
    struct sNode* stack = NULL;

    while (exp[i]){
        if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
            push(&stack, exp[i]);

        if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']') {

            if (stack == NULL){
                printf("%d: %c\n",i,exp[i]);
                return 0;
            }else if (!isMatchingPair(pop(&stack), exp[i])){
                printf("%d: %c\n",i,exp[i]);
                return 0;
            }
        }
        i  ;
    }

    if(stack == NULL){
        return 1;
    }else{
        printf("open: ");
        
            while(stack!=NULL){
             int value = pop(&stack);
            if(value == '('){
                printf("%c",')');
            }else if(value == '['){
                printf("%c",']');
            }else if(value == '{'){
                printf("%c",'}');
            }
            }
            printf("\n");
            while(stack!=NULL){
            pop(&stack);
            }
        return 0;
    }
    
}

int main(int argc, char* argv[]){
    if(argc>1){
        int n = areBracketsBalanced(argv[1]);
        if(n==0){
            return EXIT_FAILURE;
        }else if(n==1){
            return EXIT_SUCCESS;
        }   
    }
    
    //return 1;
}

CodePudding user response:

You have to ensure that all paths through areBracketsBalanced() empty the stack befoer returning. You're missing this case:

            }else if (!isMatchingPair(pop(&stack), exp[i])){
                printf("%d: %c\n",i,exp[i]);
                while (stack != NULL) {
                    pop(&stack);
                }
                return 0;
            }

Since you do this from several places, I recommend defining a clear(&stack) function instead of writing the loop every time.

  • Related