Segmentation fault error
Apr 10, 2012 at 3:20am UTC
I am getting a segmentation fault error in this code taking an infix expression to a postfix expression, I have a lot of debugging code in and I'll leave it in so you can see how far it gets as well. I can't figure out why It is doing this, I have to use my own string library for this which I will include so you can see what stack is using for the strings. This is a lot I am asking to be looked over, but if anyone has the time please assist me! :)
I apologize for string being so sloppy.
This is a link to my string class, it is too long to post in here -
http://pastebin.com/vv4tUw3w
Stack Header with implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#ifndef CS33001_STACK_H_
#define CS33001_STACK_H_
#include <cassert>
#include <new>
template <typename T>
class node {
public :
node() : data(), next(0) {};
node(T item) : data(item), next(0) {};
T data;
node<T> *next;
};
template <typename T>
class stack{
public :
//default constructor
stack(): tos(0) {};
//copy constructor
stack(const stack<T>&);
//deconstructor
~stack();
//constant time swap
void swap(stack<T>&);
//assignment operator
stack<T>& operator =( stack<T> rhs);
bool isEmpty() const {return tos == 0; };
bool isFull() const ;
T pop();
void push(const T&);
private :
node<T> *tos;
};
template <typename T>
stack<T>::stack(const stack<T>& actual) {
node<T> *ttos = actual.tos,
*bottom = 0, *temp;
tos = 0;
while (ttos != 0) {
temp = new node<T>(ttos -> data);
if (tos == 0) { //first node
tos = temp;
bottom = temp;
}
else {
bottom -> next = temp;
bottom = temp;
}
ttos = ttos -> next;
}
}
template <typename T>
stack<T>::~stack() {
while (tos != 0) {
node<T> *temp = tos;
tos = tos -> next;
delete temp;
}
}
template <typename T>
void stack<T>::swap(stack& rhs) {
node<T> *temp = tos;
tos = rhs.tos;
rhs.tos = temp;
}
template <typename T>
bool stack<T>::isFull() const {
node<T> *temp = new (std::nothrow) node<T>();
if (temp==0) return true ;
delete temp;
return false ;
}
template <typename T>
stack<T>& stack<T>::operator =( stack<T> rhs) {
swap(rhs);
return *this ;
}
template <typename T>
T stack<T>::pop() {
assert(tos != 0);
node<T> *temp = tos;
T result = tos -> data;
tos = tos -> next;
delete temp;
return result;
}
template <typename T>
void stack<T>::push(const T& item) {
assert(!isFull());
node<T> *temp = new node<T>(item);
temp -> next = tos;
tos = temp;
}
#endif
Infix to postfix where fault occurs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include "stack.h"
#include <iostream>
#include <fstream>
#include <ostream>
#include "string.h"
#include <vector>
string infix_to_postfix(string expr);
int main (int argc, char **argv) {
std::streambuf * buf;
std::ofstream of;
if (argc == 1)
{
std::cout << "Usage: Postfix input [output] " ;
return 0;
}
string input;
if (argc == 2)
{
//buffered, stored in a buffer and then read.
buf = std::cout.rdbuf();
input = string(argv[1]);
}
//If there are more then two strings
else if (argc > 2){
//If there are more then two arguments provided, take the first to be the input and the second to be the output
input = string(argv[1]);
//open the file
of.open(argv[2]);
//assigning the buffer
buf = of.rdbuf();
}
//Constructing the output object
std::ostream out(buf);
std::ifstream fin(input.get_c_str());
if (fin.fail())
{
std::cerr<<"Cannot open the input file. "
<<"Program terminates!" <<std::endl;
return 0;
}
string line;
while (fin.good()){
std::cout << "I am almost there" ;
fin >> line;
std::cout << "Infix Expression: " << line << "\n" ;
std::cout << "I am close" ;
string infix = infix_to_postfix(line);
std::cout << "Postfix Expression: " << infix << "\n" ;
}
return 0;
}
string infix_to_postfix(string expr)
{
std::cout << "I am here" ;
std::vector<string> tokens = expr.split(' ' );
std::cout <<"I am here 2" ;
for (int i = 0; i < tokens.size(); ++i)
{
std::cout << "_" << tokens[i] << "_" ;
}
stack<string> s;
int i = 0;
string lhs,rhs,op;
while (tokens[i]!= ";" ){
if (tokens[i] != "(" ){
if (tokens[i] == ")" ){
rhs = s.pop();
op = s.pop();
lhs = s.pop();
s.push(lhs + rhs + op);
}
else
s.push(tokens[i]);
}
++i;
}
string result;
while (! s.isEmpty()){
result = result + s.pop();
}
return result;
}
Apr 10, 2012 at 3:43am UTC
This section may be the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (tokens[i]!= ";" ){ //Are you guaranteed that tokens contains a ";"? If not then you will be trying to read
//uninitialized data. A fix might be: while(i < tokens.size() && tokens[i]!= ";")?
if (tokens[i] != "(" ){
if (tokens[i] == ")" ){
rhs = s.pop();
op = s.pop();
lhs = s.pop();
s.push(lhs + rhs + op);
}
else
s.push(tokens[i]);
}
++i;
}
Apr 10, 2012 at 3:52am UTC
I am guaranteed that every line will be an infix expression ending in a ;
But I was thinking it was that section causing it too
Last edited on Apr 10, 2012 at 3:53am UTC
Apr 10, 2012 at 4:08am UTC
Do you not have any idea where the error is? Try using breakpoints to narrow it down. And look at the local variables of your code to see if you have a bad pointer somewhere during suspicious functions.
Topic archived. No new replies allowed.