【问题标题】:Segmentation fault when joining two programs加入两个程序时出现分段错误
【发布时间】:2020-04-09 23:40:53
【问题描述】:

我正在编写这个程序来评估 post fix 表达式,并且程序可以自行运行良好。但是当我将这个程序与我的另一个程序一起加入时,它给了我分段错误。

像这样:

typedef struct node2
{
    int info;
    struct node2 *following;

}node2;

typedef struct eval
{
    int size;
    node2 *head1;
}eval;

int init(eval **exp)
{
    *exp = (eval*)malloc(sizeof(eval));
    if(*exp==NULL)
    {
        return EXIT_FAILURE;
    }

    (*exp)->head1 = NULL;
    (*exp)->size = 0;

    return EXIT_SUCCESS;
}

void postpush(eval *exp, int num)
{
    node2 *newn=(node2*)malloc(sizeof(node2));

    newn->following=exp->head1;
    exp->head1=newn;
    newn->info=num;
    (exp->size)++;

}

void postpop(eval *exp)
{
    node2 *postpop=NULL;

    postpop = exp->head1;
    exp->head1 = postpop->following;
    (exp->size)--;
    free(postpop);
}

int operand(char op)
{
    if (op == '1'|| op == '2'||op == '3' || op == '4' || op == '5' || op == '6' || op == '7' || op == '8' || op == '9' || op == '0' )
        return 1;
    return 0;
}

void expre(int head2, int temp, char operand, eval* exp)
{
    int result=0;

    switch(operand)
    {
    case '+':
        result=head2 + temp;
        break;
    case '-':
        result=head2 - temp;
        break;
    case '/':
        result=head2/temp;
        break;
    case '*':
        result= head2*temp;
        break;
    case '^':
        result= pow(head2,temp);
        break;
    default:
        return;
    }
    postpop(exp);
    postpush(exp,result);
}

int operators(char op)
{
    if (op == '+' || op == '-' || op == '*' || op=='/' || op=='^')
        return 1;

    return 0;
}

int evaluationpost()
{
    eval *exp = NULL;
    char input[MAX];

    init(&exp);

    char* op;

    printf("Please enter a postfix expression (with spaces between the operators and operands):");
    gets(input);

    int temp;

    op=input;

    while(*op!='\0')
    {
        if(operand(*op))
        {
            postpush(exp, abs((int)(*op)-48));
        }

        if (operators(*op))
        {
            temp=(exp->head1)->info;
            postpop(exp);
            expre((exp->head1)->info, temp, *op, exp);

        }
        op++;
    }

    printf("Result: %d\n", (exp->head1)->info);

    return 0;
}

int main()
{
    evaluationpost();
}

它会运行良好,不会有任何错误。但是一旦我将此代码输入另一个程序并尝试调用该函数,它就会给我一个“分段错误”。

这是我试图在其中调用函数评估帖子的代码部分:

case 3: //if the expression is a postfix expression
    printf("This is a postfix expression. Would you like to:\n"); //telling the user its a postfix expression
    printf("A- Convert it to infix\n");                           //and asking them what they want to do
    printf("B- Convert it to prefix\n");
    printf("C- Evaluate the expression\n");
    printf("D- Exit the program.\n");
    printf("Your choice: "); //asking the user for their choice
    scanf("%c", &option);    //getting the user's choice

    if(option=='A' || option =='a') //if the user chooses to convert the expression to infix
    {
        printf("The infix expression is: %s\n", ConvertPostfix(input, 2)); //calling the function that changes postfix to infix
                                                                           //and displaying the converted expression to the user
        //saving the output in the output.txt file
        output=ConvertPostfix(input, 2); //assigning the converted expression to output
        fprintf(PTR, "Postfix expression converted to an infix expression: %s\n", output); //putting the converted expression in the file
        printf("Output was saved to output file.\n"); //telling the user the output was saved in output.txt
        fclose(PTR); //closing the file using its pointer
    }
    else if(option=='B' || option=='b') //if the user chooses to convert the expression to prefix
    {
        printf("The prefix expression is: %s\n", ConvertPostfix(input, 1)); //calling the function that changes postfix to prefix
                                                                            //and displaying the converted expression to the user
        //saving the output in the output.txt file
        output=ConvertPostfix(input,1); //assigning the converted expression to output
        fprintf(PTR, "Postfix expression converted to an prefix expression: %s\n", output); //putting the converted expression in the file
        printf("Output was saved to output file.\n"); //telling the user the output was saved in output.txt
        fclose(PTR); //closing the file using its pointer
    }
    else if(option=='C' || option =='c') //if the user chooses to evaluate the expression
    {
        evaluationpost(); //calling the function that evaluates postfix expressions
    }

当我运行我的程序时,输入一个后缀表达式并按 C 来计算它。它给了我“分段错误”。

我不明白为什么当它自己运行良好时它会在这里给我一个分段错误。

【问题讨论】:

  • 不确定是什么问题,但不要使用gets。请改用fgets。此外,evaluationpost() 中存在内存泄漏,因为它最后没有释放exp。此外,由于堆栈上没有足够的操作数,在处理无效的后缀表达式时可能会崩溃。
  • gets 它已被弃用。 MAX 是什么?

标签: c segmentation-fault


【解决方案1】:

这一行

scanf("%c", &option);

只读取一个字符。但是当你输入c<ENTER>时,你发送两个字符,第二个是换行符\n

gets() 函数(无论如何你都不应该使用它,听听你的编译器!)

gets(input);

现在只读取(终止)换行符并存储一个空字符串(换行符被删除)。

while 条件现在立即为假,因此永远不会执行循环体:

while(*op!='\0')

现在当你取消引用时

printf("Result: %d\n", (exp->head1)->info);

那么exp->head1 仍然是NULL 指针,由您的init() 函数初始化,导致分段错误。

你必须处理空字符串的条件并使用

scanf("%c ", &option);

吞下输入缓冲区中的换行符

【讨论】:

  • 还有什么可以用来代替不读 \n 的 scanf 的吗?我试过使用fgets(option, 1, stdin);,但它甚至不允许我输入选项。我尝试使用option=getchar();,但遇到了同样的分段错误。
  • 您需要为fgets() 提供足够的空间,它存储至少一个零字节。只需提供一些字节缓冲区。一个字符的最小值,换行符和零字节为 3 个字符。
猜你喜欢
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 2013-11-30
  • 2015-03-20
  • 2013-09-13
相关资源
最近更新 更多