简体   繁体   English

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

[英]Segmentation fault in C++ with std::cin while macro is defined

I am trying to solve a problem related to stack data structure, I have an implementation of a stack, and a main method that uses it, this is a for-learning question as i am a beginner, can you guys tell me, why i get this error?: 我正在尝试解决与堆栈数据结构有关的问题,我有一个堆栈的实现,以及使用它的主要方法,因为我是初学者,这是一个有学习意义的问题,你们能告诉我,为什么我得到这个错误?:

    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

my code is the following: 我的代码如下:

#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;
}

if i set MAX_SIZE variable to a lower number the code runs well, i want to know why is that the case, how std::cin and macros interact??, i am a beginner, sorry if this is a simple question it is the first time that i am asking in stackoverflow, 如果我将MAX_SIZE变量设置为较小的数字,则代码运行良好,我想知道为什么是这种情况,std :: cin与宏如何相互作用?,我是初学者,很抱歉,如果这是一个简单的问题,那是我第一次在stackoverflow中询问时,

MAX_SIZE is far too big. MAX_SIZE太大。 MAX_SIZE determines the size of your Stack objects. MAX_SIZE确定Stack对象的大小。 As the total size of local variables in a function is limited to a few megabytes (depending on the platform), you simply exceed this size. 由于函数中局部变量的总大小限制为几兆字节(取决于平台),因此您只需超出此大小即可。

In your case you have two local Stack objects in main ( S1 and S2 ), each of them taking roughly 20 Mbytes (assuming sizeof int is 4). 在您的情况下,您在main有两个本地Stack对象( S1S2 ),每个对象大约占用20 MB(假设sizeof int为4)。

This is totally unrelated to cin though. 不过,这与cin完全无关。

Your Stack objects are allocated on the stack. 您的Stack对象分配在堆栈上。

By default the stack is limited to something like 1-8 MB per thread depending on your platform. 默认情况下,根据您的平台,每个线程的堆栈限制为1-8 MB。

Each of your stack objects takes up 20 MB so you are running out of stack space. 每个堆栈对象占用20 MB,因此您的堆栈空间不足。 To fix this change your code to: 要解决此问题,请将您的代码更改为:

std::unique_ptr<Stack> S1(new Stack());
std::unique_ptr<Stack> S2(new Stack());

This will allocate your objects on the heap which is only limited by the size of the available memory and swap space on your machine. 这将在堆上分配对象,该对象仅受计算机上可用内存和交换空间大小的限制。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM