ERROR: LeakSanitizer: detected memory leaks

ERROR: LeakSanitizer: detected memory leaks

当我 运行 我的程序时,我不断收到以下错误:

它提到了第 15、64 和 110 行的泄漏。我认为错误出在 push 函数中,但我看不出在哪里可以释放变量而不导致我的代码出错。

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

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

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

这是我的代码:

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

你必须确保通过 areBracketsBalanced() 的所有路径在返回之前清空堆栈。你错过了这个案例:

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

由于您从多个地方执行此操作,我建议定义一个 clear(&stack) 函数而不是每次都编写循环。