微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决代码中的分段错误?

如何解决如何解决代码中的分段错误?

我正在尝试解决括号匹配的问题,即给定一系列左括号和右括号 GLIBC,由 exp(、方括号 )[ 或花括号 ]},我需要判断给定的序列是否平衡。

{

PS:空字符串被认为是平衡字符串。
下面是用 C 编写的代码,其中我尝试使用 Sample Input1: exp = "[]{}()" Sample Output1: Match Successfull....! ---------- Sample Input2: exp = "[]{(]}()" Sample Output1: Match not Successfull....! 实现堆栈。我实现了堆栈的所有必要功能,即 structpop()push() 等。
但是,我遇到了分段错误。请帮我纠正错误

isFull()

解决方法

您的代码导致错误的原因是您的 exp[i] = '\0'; 循环条件中的语句 for。这在逻辑上是不正确的(解释如下),您不能修改使用 char* 声明的字符串。

为什么这么问?阅读Why do I get a segmentation fault when writing to a “char *s” initialized with a string literal,but not “char s[]”?


您的代码中还有其他几个错误:

  1. 这不是你在 c,char *exp = "[]{}()"; 中分配字符串的方式。虽然它确实有效,但会产生警告,不推荐使用。阅读Assigning strings to arrays of characters

  2. 您尚未初始化 s->size,但您正在 malloc() 的这一行 s->arr = (char *)malloc(s->size * sizeof(char)); 中使用它。

  3. 您将 push() 函数的返回类型声明为 int,但在 else 条件之后没有返回任何内容。这是未来的潜在错误。它还会导致警告。

  4. 您的 pop() 函数的实现存在逻辑和语法缺陷。 pop() 函数的返回类型是 int 但你返回的是 char 类型。此外,您首先减少 ptr->top,然后在新的 ptr->top 位置返回字符,即您最终返回低于当前顶部的字符。

  5. 这不是 exp[i] = '\0' 如何检查 for 循环的终止条件以在 c 中结束 char* 字符串。你应该写exp[i] != '\0'

  6. 你一遇到return 0;)}]。您应该检查是否可以从堆栈中弹出相应的左大括号,如果是,则检查其他字符 else return 0;

以下是修复了上述所有错误的代码。

#include <stdio.h>
#include <stdlib.h>
struct stack {
    int size;
    int top;
    char *arr;
};

int isEmpty(struct stack *ptr) {
    if (ptr->top == -1)
    {
        return 1;
    }
    return 0;
}

int isFull(struct stack *ptr) {
    if (ptr->top == ptr->size - 1)
    {
        return 1;
    }
    return 0;
}

int push(struct stack *ptr,char val) {
    if (isFull(ptr))
    {
        return 1;
    }
    else
    {
        ptr->top++;
        ptr->arr[ptr->top] = val;
    }
    return 0;
}

char pop(struct stack *ptr) {
    char val;
    if (isEmpty(ptr))
    {
        return '\0';
    }
    else
    {
        val = ptr->arr[ptr->top];
        ptr->top--;
        return val;
    }
}

int match(char a,char b) {
    if ((a == '{' && b == '}') || (a == '(' && b == ')') || (a == '[' && b == ']'))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int parMatch(char exp[]) {
    struct stack *s = malloc(sizeof(struct stack));
    s->size = 100;
    s->arr = (char *)malloc(s->size * sizeof(char));

    s->top = -1;
    char char_pop;
    for (int i = 0; exp[i] != '\0'; i++)
    {
        if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
        {
            push(s,exp[i]);
        }
        else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
        {   // You have one closing brace but your stack is empty,then return 0
            if (isEmpty(s))
            {
                return 0;
            }
            else
            {   // If you reach here then it ensures that stack is not empty.
                char_pop = pop(s);
                if (!match(char_pop,exp[i]))
                {
                    return 0;
                }
            }
        }
    }
    if (isEmpty(s))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main() {
    char exp[100] = "{}}[](";
    if (parMatch(exp))
    {
        printf("Match Successfull....!");
    }
    else
    {
        printf("Match not Successfull....!");
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。