Sign in to follow this  

Problem with pointers

Recommended Posts

I've been working on creating a stack class for a few days. Most of that time was spent learning how to overload the = operator, but I'm pretty sure I have that working. My problem is after I run a pop method to remove the first (top) element of the stack, after leaving that method the stack somehow gets assigned some crazy numbers. I think it might have something to do with passing the pointer into the << overload. Here is my Node class (I have inserted some checks in the = overload to show the values are being set equal.
class Node {
	int data;
	Node *next;
	Node() : data(0), next(0) { }
	Node(int e) : data(e), next(0) { }
	Node(int e, Node* n) : data(e), next(n) { }
	friend class Stack;
	friend ostream& operator<<(ostream&, const Node&);
	friend ostream& operator<<(ostream&, const Stack&);
	int getData(void)const{
		return data;}
	Node getNext(void)const{
		return *next;}
	void setData(int a){
	void setNext(Node* a){
	Node Node::operator =(Node& rightside){
		if ( &rightside != this){
			cout<<"left side value = "<<data<<"   right side data is "<<rightside.getData()<<endl;
			cout<<"left side next = "<<next<<"   right side next is "<<(rightside.getNext())<<endl;
			cout<<"left side value = "<<data<<"   right side data is "<<rightside.getData()<<endl;
			cout<<"left side next = "<<*next<<"   right side next is "<<(rightside.getNext())<<endl;
		return *this;

ostream& operator<<(ostream& os, const Node& n) {
	os <<;
	return os;
Here is my stack class with the pop function. I have run through this with the step by step debugger and indeed all the values are correct inside of the pop function. Even when I output the new top of the stack it is the correct value.
class Stack {
	Node *theTop;
	Stack() : theTop(0) { }
	void push(int e) { 
		Node *newone = new Node(e, theTop);
		theTop = newone;
	Node getTop()
		return *theTop;}
	int pop(void) {
		int value=theTop->getData(); //Get the data in the node we are about to delete.
		Node* temp3=new Node();
		temp3=theTop; //set theTop to a temp node so we can delete it at the end.
		Node *temp2=new Node();
		cout<<"temp2: "<<temp2->getData()<<"  "<<temp2->getNext()<<endl;

		cout<<"theTop: "<<theTop->getData()<<"  "<<theTop->getNext()<<endl;
		return value;
	friend ostream& operator<<(ostream&, const Stack&);


ostream& operator<<(ostream& os, const Stack& s) {

	os << "{ ";
	Node *temp = s.theTop;
	while (temp) {
		os << *temp << " ";
		temp = temp->next;
	os << "}";
	return os;
Here is my short main
int main( ) {
	Stack s;
	cout<<"Popping "<<s.pop()<<" from the stack."<<endl;
	return 0;
The output that I get after running it is
<40 30 20 10>     *This is the stack after everything has been pushed into it
temp2: 30 20      * This is the temp node set to the node after theTop
theTop:30 20      *This is the data in theTop and the data theTop points too.
<30 20 10>        *This is the output of the stack from within the pop function.   
                  *Everything is how it should be
popping 40 from the stack     *s.pop returns the value its popping
< -858993460 273766588 273256528>   *obviously this is not <30 20 10>
If this makes sense to anyone and you can explain to me why it out puts correctly when in the function but incorrectly when outside the function I'd be thankful.

Share this post

Link to post
Share on other sites
Your problem is in the getNext method.
It should return a pointer to a Node (or even a reference if you're that way inclined) rather than just returning a node.

Basically it's a scope problem. The Node that is returned from the function will only 'exist' until the block the function was called in completes. At this point the node is destroyed so the address of the Node becomes invalid.

So yeah, the easiest solution for you at the moment is to change getNode to return a Node*.

Also, I know you just probably haven't gotten around to doing it yet, but you should get into the habit of doing your 'deletes' sooner rather than later (i.e. implement them now so you don't forget later and end up with nasty leaks all over the place). And never lose track of where your pointers are looking in memory.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this