c++ - 在定义宏时使用 std::cin 在 C++ 中出现段错误

标签 c++ segmentation-fault cin

我正在尝试解决与堆栈数据结构相关的问题,我有一个堆栈的实现,以及一个使用它的主要方法,这是一个学习问题,因为我是初学者,你们能告诉我吗,为什么我会收到此错误?:

    GDB trace:
Reading symbols from solution...done.
[New LWP 24202]
Core was generated by `solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  main () at solution.cc:70
70      cin >> N;
#0  main () at solution.cc:70

我的代码如下:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX_SIZE 5000000

class Stack
{
private:
    int A[MAX_SIZE];  // array to store the stack
    int top;   // variable to mark the top index of stack. 
public:
    // constructor
    Stack()
    {
        top = -1; // for empty array, set top = -1
    }

    // Push operation to insert an element on top of stack. 
    void Push(int x) 
    {
      if(top == MAX_SIZE -1) { // overflow case. 
            printf("Error: stack overflow\n");
            return;
        }
        A[++top] = x;
    }

    // Pop operation to remove an element from top of stack.
    void Pop() 
    {
        if(top == -1) { // If stack is empty, pop should throw error. 
            printf("Error: No element to pop\n");
            return;
        }
        top--;
    }

    // Top operation to return element at top of stack. 
    int Top() 
    {
        return A[top];
    }

    // This function will return 1 (true) if stack is empty, 0 (false) otherwise
    int IsEmpty()
    {
        if(top == -1) return 1;
        return 0;
    }

    // ONLY FOR TESTING - NOT A VALID OPERATION WITH STACK 
    // This function is just to test the implementation of stack. 
    // This will print all the elements in the stack at any stage. 
    void Print() {
        int i;
        printf("Stack: ");
        for(i = 0;i<=top;i++)
            printf("%d ",A[i]);
        printf("\n");
    }
};

int main() {

    int N;
    cin >> N;

    Stack S1;
    Stack S2;


    for(int i = 0; i < N; i++)
    {
        int q;
        cin >> q;

        if(q == 1)
        {
            int x;
            cin >> x;

            if(S1.IsEmpty() || S2.IsEmpty())
            {
                S1.Push(x);
                S2.Push(x);
            }
            else
            {
                S1.Push(x);
                if(x >= S2.Top()) S2.Push(x);                
            }
        }

        if(q==2)
        {
            if(S1.Top() == S2.Top())
            {
                S1.Pop();
                S2.Pop();
            }else
            {
                S1.Pop();
            }
        }

        if(q==3)
        {
            cout << S2.Top() << endl;
        }
    }


    return 0;
}

如果我将 MAX_SIZE 变量设置为较小的数字,代码运行良好,我想知道为什么会这样,std::cin 和宏如何交互??,我是初学者,抱歉,如果这是一个简单的问题这是我第一次在 stackoverflow 中提问,

最佳答案

MAX_SIZE 太大了。 MAX_SIZE 确定您的 Stack 对象的大小。由于函数中局部变量的总大小限制为几兆字节(取决于平台),您只需超出此大小即可。

在您的情况下,您在 main 中有两个本地 Stack 对象(S1S2),每个它们大约占用 20 MB(假设 sizeof int 为 4)。

虽然这与 cin 完全无关。

关于c++ - 在定义宏时使用 std::cin 在 C++ 中出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50257031/

相关文章:

gcc 7.5 上的 C++ 隐式转换和通用转发

c函数重复执行而不被调用,导致段错误

c++ - 为什么不能给指针赋值?

c++ - std::cin.fail()问题

c++ - 如何在 C++ 中读取一行并用 '!!"分隔它?

c++ - mingw g++ vector<T>::插入错误

c++ - 如果没有单击按钮,MFC 鼠标运动 OnMouseMove 事件 nFlags 值?

c++ - 以下结合了 erase 和 remove 的语法是什么意思?

c++ - SPOJ 简单动态规划中的 SIGSEGV 错误

c++ - 在 C++ 中添加类?