简体   繁体   English

运行时检查失败2:变量'expression'周围的堆栈已损坏

[英]Run-Time Check Failure #2: Stack around the variable 'expression' was corrupted

Following is my code for converting infix to postfix notation and later postfix is also being evaluated. 以下是我将infix转换为postfix表示法的代码,以后的postfix也正在评估中。 This code works fine and gives right answer but in the end I am getting this error on run time. 这段代码可以正常工作并给出正确的答案,但最后我在运行时遇到了此错误。

Why this error is being caused? 为什么会导致此错误? I am using Viusal C++ 2010. 我正在使用Viusal C++ 2010.

在此处输入图片说明

Code: 码:

#include<iostream>
#include<stack>
#include<string.h>

using namespace std;


int getPrecedence( char tmp )
{
    if(tmp=='+')
    {
        return 1;
    }
    else if(tmp == '-')
    {
        return 1;
    }
    else if(tmp == '*')
    {
        return 2;
    }
    else if(tmp == '/')
    {
        return 2;
    }
    else if(tmp == '=')
    {
        return 0;
    }
    else if(tmp == '(')
    {
        return -1;
    }
    else if(tmp == ')')
    {
        return -1;
    }
}

int main()
{

    stack<char> st;

    char expression[10];

    cout<<"Enter expression : ";
    //cin>>expression;
    strcpy(expression,"x=(0+1)+(y=2+3)");
    char postfix[100];  // its postfix string
    int counter=0;

    int i=0;

    int bracketCheck = 0;

    //(a+b)
    while( expression[i] != '\0' )  // iterate till '/0' does not come.
    {
        if(expression[i]== '+' || expression[i]== '-' || expression[i]== '*' || expression[i]== '/' || expression[i]== '='  )
        {
            if( st.empty() )
            {
                st.push(expression[i]);
            }
            else // when stack not empty
            {
                int topPrecedence = getPrecedence( st.top() );
                int expressionPrecedence = getPrecedence( expression[i] );


                while( !(topPrecedence < expressionPrecedence) )
                {
                    postfix[counter++] = st.top();
                    st.pop() ;

                    if(! st.empty() )
                    {
                        topPrecedence = getPrecedence( st.top() );
                    }
                    else
                    {
                        break;
                    }


                }

                //if( st.empty() )
                //{
                //  st.push( expression[i] );
                //}

                if( topPrecedence < expressionPrecedence )
                {
                    st.push( expression[i] );
                }


            }
        }
        else if( expression[i]=='(' )
        {
            st.push( expression[i] );
            bracketCheck++;
        }
        else if( expression[i]==')' )
        {
            int topPrecedence = getPrecedence( st.top() );
            int expressionPrecedence = getPrecedence( expression[i] );

            while( topPrecedence >= expressionPrecedence)   // +>=) --- 1 >= -1 ------- +>=) --- -1 >= -1
            {

                if( getPrecedence( st.top() ) != -1 )
                {
                    postfix[counter++] = st.top();
                }
                char BracketFound = st.top();
                st.pop();
                if( !st.empty() )
                    topPrecedence = getPrecedence( st.top() );
                if( st.empty() )   // break out of loop when stack is empty
                    break;
                if( BracketFound == '(' )
                    break;

            }

        }
        else // when its an alphabet 
        {
            postfix[counter++] = expression[i];
        }


        i++;
    } // outer while ends 

    while( ! st.empty() )
    {
        postfix[counter++] = st.top();
        st.pop();
    }


    postfix[counter] = '\0';
    i=0;

    while( postfix[i] != '\0' )
    {
        cout<<postfix[i]<<" ";
        i++;
    }

    stack<char> e; // eval stands for evaluation
    i=0;

    while( postfix[i] != '\0' )
    {
        if( postfix[i] == '+' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left + right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );

        }
        else if( postfix[i] == '-' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left - right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );
        }
        else if( postfix[i] == '*' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left * right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );
        }
        else if( postfix[i] == '/' )
        {
            int right = e.top();
            right = right - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            left = left - 48;
            e.pop();

            int result = left / right;
            result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( result );
        }
        else if( postfix[i] == '=' )
        {
            int right = e.top();
            //left = left - 48;  // 48 is ascii of 0 so 49-48=1
            e.pop();

            int left = e.top();
            //right = right - 48;
            e.pop();


            //int result = right + left;
            //result = result + 48;   // sets to right ascii and in next line this ascii is type casted to char.
            e.push( right );

        }
        else
            e.push( postfix[i] );
        i++;
    }

    // while ends

    cout<<endl;
    cout<<"result= "<<e.top()<<endl;





    system("pause");
    return 0;
}

Well, the first problem I see is that you are copying 15 characters + null terminator into expression[10]. 好吧,我看到的第一个问题是您正在将15个字符+空终止符复制到expression [10]中。 Basically, you are stuffing 16 pounds into a 10-pound sack. 基本上,您将16磅装满10磅重的麻袋。

char expression[10];

//<snip>

strcpy(expression,"x=(0+1)+(y=2+3)");

Look at the length of what you're copying into expression (don't forget the trailing \\0 ). 查看要复制到expression的内容的长度(不要忘了尾随\\0 )。 You've allocated 10 spaces in your array, but you're trying to store 16 in it, and you're overflowing your buffer. 您已经在数组中分配了10个空间,但是您试图在其中存储16个空间,并且缓冲区溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM