定义宏时 C++ 中的分段错误 std::cin
Segmentation fault in C++ with std::cin while macro is defined
我正在尝试解决与堆栈数据结构相关的问题,我有一个堆栈的实现,以及一个使用它的主要方法,这是一个学习问题,因为我是初学者,你们可以吗告诉我,为什么我会收到这个错误?:
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 和宏如何交互??,我是初学者,抱歉,如果这是一个简单的问题,这是我第一次在 Whosebug 中提问,
MAX_SIZE
太大了。 MAX_SIZE
确定 Stack
对象的大小。由于函数中局部变量的总大小限制为几兆字节(取决于平台),您只需超过此大小即可。
在您的情况下,您在 main
中有两个本地 Stack
对象(S1
和 S2
),每个对象占用大约 20 MB(假设 sizeof int
是 4).
虽然这与 cin
完全无关。
您的 Stack
个对象分配在堆栈上。
默认情况下,堆栈限制为每个线程 1-8 MB,具体取决于您的平台。
您的每个堆栈对象占用 20 MB,因此您 运行 超出堆栈 space。要解决此问题,请将您的代码更改为:
std::unique_ptr<Stack> S1(new Stack());
std::unique_ptr<Stack> S2(new Stack());
这将在堆上分配您的对象,堆上仅受可用内存大小的限制,并在您的计算机上交换 space。
我正在尝试解决与堆栈数据结构相关的问题,我有一个堆栈的实现,以及一个使用它的主要方法,这是一个学习问题,因为我是初学者,你们可以吗告诉我,为什么我会收到这个错误?:
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 和宏如何交互??,我是初学者,抱歉,如果这是一个简单的问题,这是我第一次在 Whosebug 中提问,
MAX_SIZE
太大了。 MAX_SIZE
确定 Stack
对象的大小。由于函数中局部变量的总大小限制为几兆字节(取决于平台),您只需超过此大小即可。
在您的情况下,您在 main
中有两个本地 Stack
对象(S1
和 S2
),每个对象占用大约 20 MB(假设 sizeof int
是 4).
虽然这与 cin
完全无关。
您的 Stack
个对象分配在堆栈上。
默认情况下,堆栈限制为每个线程 1-8 MB,具体取决于您的平台。
您的每个堆栈对象占用 20 MB,因此您 运行 超出堆栈 space。要解决此问题,请将您的代码更改为:
std::unique_ptr<Stack> S1(new Stack());
std::unique_ptr<Stack> S2(new Stack());
这将在堆上分配您的对象,堆上仅受可用内存大小的限制,并在您的计算机上交换 space。