Thread: This is really weird..

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    This is really weird..

    I just wrote a a stack implementation using linked list..
    when i pop the values it is as expected

    Code:
    	stack.push(7);
    	stack.push(8) ;
    	stack.push(9) ;
    	cout << stack.pop()  << endl << stack.pop() << endl ;
    when I pop the values..it prints out as 7 8 9! it only prints correctly if I put the print statements on different lines..literally. It doesn't make sense to me..
    i debugged and the first pop definitely returns a 9..so how does it end up being at the end?
    here is the code.. .cpp file
    Code:
    #include "StackLinkedList.h"
    Stack::Stack()
    {
    	size=0;
    	stackPtr = NULL;
    }
    Stack::~Stack()
    {
    	delete stackPtr ;
    }
    
    void Stack::push(int x)
    {
    	++size ;
    
    	Node *temp ;
    	temp= stackPtr;
    	stackPtr = new Node(x) ;
    	stackPtr->next =temp;
    }
    bool Stack::empty()
    {
    
    	return (stackPtr==NULL) ;
    
    }
    int Stack::pop()
    {
    	
    		int n;
    		Node *t = stackPtr ;
    		stackPtr = stackPtr->next ;
    		n = t->data;
    
    		delete t ;
    		return n ;
    
    
    }
    You ended that sentence with a preposition...Bastard!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's because your program has undefined behaviour.

    You're modifying the same object (your stack) more than once between sequence points.

    It's like the old
    cout << a++ << a++ << endl;
    and wondering why the result is one of many possible things you didn't expect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    so there is a bug in the code..aargh, how will I find it..I can't spot anything!
    You ended that sentence with a preposition...Bastard!

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    One thing that i noticed is;
    You are increasing the size of linked list everytime you push but you don't decrease it when you pop out...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You do
    a = pop();
    b = pop();
    cout << a << b;
    so that you introduce some sequence points into the code to make it explicit what is happening.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Salem View Post
    You do
    a = pop();
    b = pop();
    cout << a << b;
    so that you introduce some sequence points into the code to make it explicit what is happening.
    yep, I did that and that works as well...
    but it should be able to chain the function calls return value..without the behavior being undefined..
    is it the problem with my code..or a cout issue which i doubt..
    You ended that sentence with a preposition...Bastard!

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Eman View Post
    yep, I did that and that works as well...
    but it should be able to chain the function calls return value..without the behavior being undefined..
    is it the problem with my code..or a cout issue which i doubt..
    You can use two functions in the same "sequence points", as long as they don't have side-effects that influence each other. Look at this:
    Sequence point - Wikipedia, the free encyclopedia

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Definitely, it's an issue with your written code...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's undefined - there is nothing more to say.

    It's not specific to cout, you could do this, and still be in the same trouble.
    answer = pop() / pop();
    There is no way for you to know which number is going to get divided by which number.

    It's just the way the language works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    brilliant..i guess i have to read the Sequence point stuff
    so Mr 777 how do you suggest I fix it..because this was the algorithm I was given..
    You ended that sentence with a preposition...Bastard!

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    First, try to understand the concept of stack. (If you don't know)
    Second, try to understand your problem specification, i mean what exactly you want to do.
    Thrid, take a copy and pen, start making design of your class, objects and different operations. I mean check all cases that what your program should do in order to solve given problem in all scenarios, you can come up with.
    Finally, write code, and test it with all possible scenarios you've made while designing and then take some other scenarios (try to crash your written program in every possible manner).
    If still problem exists, throw it here and we all will try our best to do what we could.

    So, i'll recommend you to write your own code. Belive me, once you understood it well, it'll not take more than 10 minutes to you to develop a working stack class but good designing and understanding is the must thing....

    Good luck.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    em...thanks for the advice, but it doesn't really help.
    I wrote this code.. And I don't write code unless I don't understand the algorithm..I have read about the stack..I have watched videos on stack..and I have accidentally found a bug in the freaking code...although that doesn't tell me where it is or how to remove it

    ah screw it.. i will use Salem's idea
    You ended that sentence with a preposition...Bastard!

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Simply don't call pop() more than once in a single expression (where the order matters).

    Code:
    cout << stack.pop() << '\n';
    cout << stack.pop() << '\n';
    Normally you use that function in a loop anyway.

    Code:
    while (!stack.empty()) 
        cout << stack.pop() << '\n';
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird problem with an array of vector<char *>'s ???
    By phummon in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2010, 05:39 PM
  2. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  3. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  4. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  5. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM