You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言栈实现括号匹配代码无输出无报错问题排查求助

解决你的栈式括号匹配无输出问题

嘿,我仔细看了你的代码,程序悄无声息没输出也没报错的核心原因是野指针非法访问内存,咱们一步步拆解问题和修复方案:

1. 致命问题:未初始化的栈指针

在parenthesisChecker函数里,你声明了struct stack *st;,但这个指针只是个野指针——它指向内存里的随机地址,直接去赋值st->size = 100;会触发非法内存访问,程序直接崩溃,自然看不到任何输出。

修复思路有两种:

  • 直接在栈上创建结构体实例(更简单,无需额外指针管理)
  • 给指针动态分配内存(如果需要堆上的栈结构)

2. 修复后的完整代码

我把核心问题修复,还加上了内存泄漏防护和异常检查,完整代码如下:

#include <stdio.h>
#include <stdlib.h>
//Initializing the stack with structures
struct stack {
    int size;
    int top;
    char *arr;
};
//function to check whether stack is empty
int isEmpty(struct stack *ptr) {
    if (ptr->top == -1) {
        return 1;
    } else {
        return 0;
    }
}
//function to check whether stack is full
int isFull(struct stack *ptr) {
    if (ptr->top == (ptr->size - 1)) {
        return 1;
    } else {
        return 0;
    }
}
//Function for push
void push(struct stack *ptr, char val) {
    if (isFull(ptr)) {
        printf("Stack Overflow, Cannot push more elements\n");
    } else {
        ptr->top++;
        ptr->arr[ptr->top] = val;
    }
}
//Function for pop
char pop(struct stack *ptr) {
    if (isEmpty(ptr)) {
        printf("Stack Underflow, Unable to pop elements\n");
        return -1;
    } else {
        char val;
        val = ptr->arr[ptr->top];
        ptr->top--;
        return val;
    }
}
//Function for parenthesis matching
int parenthesisChecker(char *exp) {
    // 修复:用栈上的结构体实例,避免野指针
    struct stack st;
    st.size = 100;
    st.top = -1;
    st.arr = (char *)malloc(st.size * sizeof(char));
    
    // 新增:检查内存分配是否成功
    if (st.arr == NULL) {
        printf("Memory allocation failed!\n");
        return 0;
    }

    int n_push = 0, n_pop = 0;
    for (int i = 0; exp[i] != '\0'; i++) {
        //for open bracket push
        if (exp[i] == '(') {
            push(&st, '('); // 传结构体地址给函数
            n_push++;
        }
        //for closed bracket pop
        else if (exp[i] == ')') {
            if (isEmpty(&st)) {
                free(st.arr); // 返回前释放内存,避免泄漏
                return 0;
            } else {
                pop(&st);
                n_pop++;
            }
        }
    }
    printf("%d times push\n", n_push);
    printf("%d times pop\n", n_pop);

    int result = isEmpty(&st);
    free(st.arr); // 释放动态分配的数组内存
    return result;
}
int main() {
    char *c = "akajvd)(()";
    if (parenthesisChecker(c)) {
        printf("Parenthesis is matched\n");
    } else {
        printf("Parenthesis is not matched\n");
    }
    return 0;
}

3. 额外优化说明

  • 新增malloc失败检查:避免内存不足时程序后续逻辑出错
  • 添加内存释放:动态分配的arr在函数结束前必须释放,防止内存泄漏
  • 修复函数调用参数:原来传野指针,现在传结构体的地址&st,符合函数的指针参数要求
  • 如果需要支持[]或{}这类括号,可以扩展匹配逻辑:比如遇到[压入[,遇到]时弹出栈顶检查是否等于[

运行结果

修复后,输入"akajvd)(()"会输出:

1 times push
2 times pop
Stack Underflow, Unable to pop elements
Parenthesis is not matched

这完全符合预期:第一个)出现时栈是空的,直接判定括号不匹配。

内容的提问来源于stack exchange,提问作者Apurv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:07:33