在函数中返回语句中的seg故障
Seg Fault at return statement in function
我的程序应该将提示从infix转换为后缀。到目前为止,通过调试器和其他各种方法,我找到了我所掌握的确切点,但不明白为什么。
这是我的代码:
这是itop.h:
using namespace std;
#include <cstdlib>
#include <iostream>
class sNode{
public:
char data;
sNode *next;
};
class stack{
public:
sNode *head;
void push (char);
sNode pop();
int rank(char);
stack()
{
cout << "Initiliazing stack." << endl;
}
};
这是我的itop.cpp文件:
#include "itop.h"
void stack::push (char a)
{
// cout << "Pushing " << a << endl;
sNode *sn;
sn = new sNode;
sn->data = a;
sn->next = head;
head = sn;
}
sNode stack::pop()
{
// cout << "Popping stack." << endl;
sNode *sn;
sn = head;
head = head->next;
return *sn;
}
int stack::rank(char x)
{
int num = 0;
// cout << "Checking rank." << endl;
if(x == ' ')
{
num = 1;
// cout << "Checking for null" << endl;
return num;
}
else if(x == '+' || x == '-')
{
num = 2;
// cout << "Checking if + or -" << endl;
return num;
// cout << "After return." << endl;
}
else if(x == '*' || x == '/')
{
num = 3;
// cout << "Checking for * or /" << endl;
return num;
}
else
cout << "Error! Input not valid!" << endl;
}
这是main.cpp:
using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "itop.h"
int main()
{
char *temp1; //Instantiating variables.
char *temp2;
temp1 = new char[20];
temp2 = new char [20];
stack s;
do //Checking commands.
{
cout << "infix_to_postfix> ";
cin >> temp1;
if(strcmp(temp1, "quit") == 0)
{
return 0;
}
if(strcmp(temp1, "convert") != 0)
{
cout << "Error! Invalid command." << endl;
}
cin >> temp2;
if(strcmp(temp1, "convert") == 0)
{
for(int i=0; i<sizeof(temp2); i++)
{
if(isdigit(temp2[i]))
{
cout << atoi(&temp2[i]);
}
else if(s.rank(temp2[i]) < s.rank(s.head->data))
{
sNode temp = s.pop();
cout << temp.data;
}
else
{
s.push(temp2[i]);
}
}
}
else
{
cout << "Error! Command not supported." << endl;
}
}while(strcmp(temp1, "quit") != 0);
return 0;
}
该函数在
上调用else if(s.rank(temp2[i]) < s.rank(s.head->data))
问题在这里:
else if(x == '+' || x == '-')
{
num = 2;
// cout << "Checking if + or -" << endl;
return num;
// cout << "After return." << endl;
}
特别是在返回num之前,我会收到"分段故障(核心倾倒)"错误消息。我已经使用了GDB,我所知道的是,在"检查 或 - "我看到" $ 1 = 2"之后。我不太确定这是什么意思,但这是我要返回的。
谢谢您的帮助。
您的代码中有很多错误。您的堆栈实现是错误的。例如,push()
仅一遍又一遍地设置head
。这会导致您的堆栈类只能容纳一个元素。next
永远不会设置为任何东西,因此它包含随机垃圾。在此外,您有:
for(int i=0; i<sizeof(temp2); i++)
sizeof(temp2)
不会给您字符串temp2
点的字符量。它为您提供指针temp2
本身的大小。此外,您最终从一个空堆栈中读取s.head
,这将是指向随机垃圾的指针。那时,所有赌注当然都没有。除了崩溃和燃烧外,您什么都不期望。
修复1:编写合适的构造函数。
stack()
{
head=NULL;
cout << "Initiliazing stack." << endl;
}
修复2:写一个额外的方法来检查堆栈是否为空。
int stack::empty()
{
if(head == NULL)
return true;
else
return false;
}
修复3:在使用堆栈数据之前检查是否堆叠为空。
else if(!s.empty() && s.rank(temp2[i]) < s.rank(s.head->data))
{
...
}
修复4:修复代码逻辑的其余部分。
相关文章:
- C++函数过载会导致 SEG 故障
- 带升压的 SEG 故障::make_shared / 特征3 内存.h.
- C ;使用lambdas在类中有条件地扩展功能(MWE的SEG故障)
- lambda回调中Android Cocos2D-X应用程序上的SEG故障
- 访问索引指向向量元素时的seg故障 - 做错了什么
- 如何修复strlen seg故障
- 将内存分配给指针数组时SEG故障
- MPI Hello World上的Valgrind SEG故障
- 迭代器越来越多,并导致SEG故障
- 复制CORBA ::任何包含字符串的corba程序SEG故障
- 当有1个以上的对象时,动态字符串类断裂与SEG故障
- C AVX2:访问数组数组中的地址时SEG故障
- SEG故障取决于指针分配的顺序
- 为什么C 线引起SEG故障
- std::map 的比较器函数中的 SEG 故障/未定义行为
- C/C++ 字符串错误与 ARM SEG 故障总线错误
- Taglib设置专辑图片,但给出了SEG故障
- 低内存会导致本机代码中的SEG故障
- STD上的SEG故障::设置整数插入
- SEG故障仅发生每次跑步