Hello everyone,

I've used this website many times to help myself based off of others' questions, but now I have a problem of my own that I can not solve. I was assigned the task of creating a stack program in C++ using a linked list. Seems simple enough, but I keep getting a segmentation fault error.

The program runs fine with variables that I define and push myself, but when I use input from a file the pop function no longer works correctly and I receive a segmentation fault.

The function at the bottom of the main file is to convert infix notated equations to postfix
eg: 1 + 3 would be 1 3 +

Here is my main file:

#include "stack.cpp"
int main(){
	stack<int> intmain;
	stack<char> charmain;

	intmain.push(1);
	intmain.push(2);
	intmain.push(3);
	std::cout << "intmain pop = " << intmain.pop() << std::endl
	<< std::endl;
	std::cout << "intmain pop = " << intmain.pop() << std::endl
        << std::endl;
	std::cout << "intmain pop = " << intmain.pop() << std::endl
        << std::endl;

	char a = 'a', b = 'b', c = 'c';
	charmain.push(a);
	charmain.push(b);
	charmain.push(c);
	std::cout << "charmain pop = " << charmain.pop() << std::endl
	<< std::endl;
	std::cout << "charmain pop = " << charmain.pop() << std::endl
        << std::endl;
	std::cout << "charmain pop = " << charmain.pop() << std::endl
        << std::endl;

	stack<char> infixtest, tempstorage;
	char right = 0, left = 0, oper = 0;
	char temp = 0;
	std::ifstream in("data3-1.txt");
	while (!in.eof()){
		in >> temp;
		if(temp == ')'){
			right = tempstorage.pop();
			std::cout << "popped :" << right << "to right." << std::endl;
			oper = tempstorage.pop();
			std::cout << "popped :" << right << "to oper." << std::endl;
			left = tempstorage.pop();
			std::cout << "popped :" << right << "to left." << std::endl;
			infixtest.push(left);
			infixtest.push(right);
			infixtest.push(oper);
		}
		else{
			if(temp != '(' && temp != ' '){
				tempstorage.push(temp);
				temp = 0;
			}
		}
	}

	return 0;
}

and here is the source file:

#include "stack.h"

template <typename T>
stack<T>::~stack(){
	while(tos != 0){
		node<T> *temp = tos;
		tos = tos -> next;
		delete temp;
	}
}

template <typename T>
stack<T>::stack(stack<T>& actual){
	node<T> *atemp, *temp;
	tos = 0;
	atemp = actual.tos;
	while ( atemp != 0 ){
		if ( tos == 0 ){
			tos = new node<T>(atemp -> data);
			temp = tos;
		}else{
			temp -> next = new node<T>(atemp->data);
			temp = temp->next;
		}
		atemp = atemp -> next;
	}
}

template <typename T>
void stack<T>::swap(stack<T>& B){
	node<T> *temp = tos;
	tos = B.tos;
	B.tos = temp;
}

template <typename T>
stack<T>& stack<T>::operator = (stack<T> rhs){
	swap(rhs);
	return *this;
}

template <typename T>
bool stack<T>::isFull() const{
	try{
		node<T> *temp = new node<T>();
		delete temp;
		return false;
	}
	catch (...) { return true; }
}

template <typename T>
void stack<T>::push (T item){
	try{
		std::cout << "pushing " << item << std::endl;
		if (isFull()) std::cout << "push error";
		node<T>* temp = new node<T>();
		temp -> data = item;
		temp -> next = tos;
		tos = temp;
	}

	catch (...){
		std::cout << "Stack overflowed trying to push: "
		<< item << std::endl << std::endl;
	};
};

template <typename T>
T stack<T>::pop () {
	node<T>* temp;
	try{
		if (isEmpty()) std::cout << "Stack is empty." << std::endl;
		temp = tos;
		if (tos -> data == 0){
			return 0;
		}
		else{
			tos = tos -> next;
			return temp -> data;
		}
	}

	catch ( char * str ){
		std::cout << "Exception: Stack underflow -- "
		<< str << std::endl << std::endl;
	};
	return -1;
};

any ideas/suggestions would be greatly appreciated!

I can see a few problems:
1. You don't delete the nodes that you pop, that is a memory leak.
2. I don't see a default constructor that sets the initial value of "tos" to NULL, if that is not done, it will surely lead to a segmentation fault if you try to pop more elements than there are on your stack (and based on the logic of your math operation inversion, that could certainly happen).
3. The function isFull is really quite useless.

You should probably use a debugger for this, with breakpoints and stack inspectors, you should find this error very easily by stepping through the code.

Thank you for the response.

The default constructor is in the header file that I did not include. I probably should have mentioned that.

I will try the advice about deleting popped elements, I just wanted to clarify the second point.

I don't know if you've solved your segment fault problem but I found sussman's comment very useful for my segment fault. Here is the link: http://www.daniweb.com/forums/thread326015.html

I also want to point out that a stack data structure shouldn't have a swap function. The whole point of a stack is first in first out.

The problem was in the pop function. I didn't initialize temp properly and delete it properly so it led to a segmentation fault down the line. Thank you all for the help, it is very appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.