Thread: Getting garbage...[Queue question]

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Getting garbage...[Queue question]

    Input does exactly what it looks like... Some user enters a string of characters and that is then appended char by char to preQ....

    This is code from my main, I bolded that cout because it doesn't even get there... It just prints garbage, and it doesn't iterate at all... So there's a problem with retrieve/append I believe... But I'm not sure what.

    Code:
    void input (Queue preQ)
    {
    	string expression;
    	int i;
    
    	cout<<"Enter a prefix expression: ";
    	cin>>expression;
    
    	for (i=0; i<=expression.length(); ++i)
    	{
    		preQ.append(expression[i]);
    	}
    }
    
    void pre_to_post (Queue preQ, Queue postQ)
    {
    	Stack opStack;
    	Stack_entry qItem; //queue item
    	Stack_entry sItem; //stack item
    
    	do
    	{
    		preQ.retrieve(qItem.element);
    		preQ.serve();
    		cout<<qItem.element;
    		if(is_operator(qItem.element)==1)
    			opStack.push(qItem);
    		else
    		{
    			postQ.append(qItem.element);
    			opStack.top(sItem);
    			opStack.pop();
    			if(sItem.flag==0)
    			{
    				sItem.flag=1;
    				opStack.push(sItem);
    			}
    			else
    			{
    				postQ.append(sItem.element);
    			}
    		}
    	}while (!(preQ.empty()));
    
    }
    Stack Class:
    Code:
    // Stack.cpp
    
    #include "Stack.h"
    
    Error_code Stack::push(const Stack_entry &item)
    /*Pre: None
      Post: If the Stack is not full, item is added to the top of the Stack.
      If the Stack is empty, an Error_code of underflow is returned.*/
    {
    	Error_code outcome = success;
    	if (myTop >= MAXSTACK-1)
    		outcome = overflow;
    	else
    		entry[myTop++] = item;
    	return outcome;
    }
    
    Error_code Stack::pop()
    /*Pre: None
      Post: If the Stack is not empty, the top of the Stack is removed.
      If the stack is empty, an Error_code of underflow is returned.*/
    {
    	Error_code outcome = success;
    	if (myTop == -1)
    		outcome = underflow;
    	else --myTop;
    	return outcome;
    }
    
    Error_code Stack::top(Stack_entry &item) const
    /*Pre: None
      Post: If the Stack is not empty, the top of the Stack is returned
      in item. If the Stack is empty an Error_code of underflow is
      returned.*/
    {
    	Error_code outcome = success;
    	if (myTop == -1)
    		outcome = underflow;
    	else
    		item = entry[myTop];
    	return outcome;
    }
    
    bool Stack::empty() const
    /*Pre: None
      Post: If the Stack is empty, true is returned. Otherwise false
      is returned.*/
    {
    	bool outcome = true;
    	if (myTop > -1) outcome = false;
    	return outcome;
    }
    
    Stack::Stack()
    /*Pre: None
      Post: The stack is initialized to be empty.*/
    {
    	myTop = -1;
    }
    Queue class:
    Code:
    // Queue.cpp
    
    #include "Queue.h"
    
    Queue::Queue()
    /*Post: The Queue is initialized to be empty. */
    {
    	count = 0;
    	rear = maxqueue -1;
    	front = 0;
    }
    
    bool Queue::empty() const
    /*Post: Return true if the Queue is empty, otherwise
      return false.*/
    {
    	return count == 0;
    }
    
    Error_code Queue::append(const Queue_entry &item)
    /*Post: item is added to the rear of the Queue. If
      the Queue is full return an Error_code of overflow 
      and leave the Queue unchanged. */
    {
    	if (count>=maxqueue) return overflow;
    	count++;
    	rear = ((rear + 1)== maxqueue)?0:(rear+1);
    	entry[rear] = item;
    	return success;
    }
    
    Error_code Queue::serve()
    /*Post: The front of the Queue is removed. If
      the Queue is empty return an Error_code of
      underflow. */
    {
    	if (count <= 0) return underflow;
    	count--;
    	front = ((front+1) == maxqueue)?0:(front+1);
    	return success;
    }
    
    Error_code Queue::retrieve(Queue_entry &item) const
    /*Post: The front of the Queue retrieved to
      the output parameter item. If the Queue is
      empty return an Error_code of underflow. */
    {
    	if (count <= 0) return underflow;
    	item = entry[front];
    	return success;
    }
    [edit] - Agh! forgot the header files, sorry!!

    Stack.h
    Code:
    // Stack.h
    
    #ifndef STACK_H
    #define STACK_H
    #include "Utility.h"
    
    const int MAXSTACK = 10;
    
    struct Stack_entry
    {	char element;
    	bool flag;
    };
    
    class Stack {
    public:
    	Stack();
    	bool empty() const;
    	Error_code pop();
    	Error_code top(Stack_entry &item) const;
    	Error_code push(const Stack_entry &item);
    
    private:
    	int myTop; //myTop is -1 when stack is empty
    	Stack_entry entry[MAXSTACK];
    };
    #endif
    Queue.h
    Code:
    // Queue.h
    
    #ifndef QUEUE_H
    #define QUEUE_H
    
    #include "Utility.h"
    
    const int maxqueue = 10; // small value for testing
    
    typedef char Queue_entry;
    
    class Queue {
    public:
    	Queue();
    	bool empty() const;
    	Error_code serve();
    	Error_code append(const Queue_entry &item);
    	Error_code retrieve(Queue_entry &item) const;
    private:
    	int count;
    	int front, rear;
    	Queue_entry entry[maxqueue];
    };
    #endif
    I've looked it over and over but I can't seem to spot the problem.
    Last edited by Sparrowhawk; 03-09-2009 at 07:13 PM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Code:
    Error_code Stack::top(Stack_entry &item) const
    /*Pre: None
      Post: If the Stack is not empty, the top of the Stack is returned
      in item. If the Stack is empty an Error_code of underflow is
      returned.*/
    {
    	Error_code outcome = success;
    	if (myTop == -1)
    		outcome = underflow;
    	else
    		item = entry[myTop];
    	return outcome;
    }
    myTop is always the element after the top, so the top element is entry[myTop - 1].

    It's interesting that it's not getting to the line containing the cout object. It seems that that is the only way anything gets to stdout, so if it's printing garbage, that would seem to be where it's coming from, unless I'm missing something?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tjb View Post
    Code:
    Error_code Stack::top(Stack_entry &item) const
    /*Pre: None
      Post: If the Stack is not empty, the top of the Stack is returned
      in item. If the Stack is empty an Error_code of underflow is
      returned.*/
    {
    	Error_code outcome = success;
    	if (myTop == -1)
    		outcome = underflow;
    	else
    		item = entry[myTop];
    	return outcome;
    }
    myTop is always the element after the top, so the top element is entry[myTop - 1].

    It's interesting that it's not getting to the line containing the cout object. It seems that that is the only way anything gets to stdout, so if it's printing garbage, that would seem to be where it's coming from, unless I'm missing something?
    In this case myTop represents the real value of the top... i.e. -1 if nothing has been put on... and 0 once something is on. That is my intention anyway, so the original should be correct as is if I want it to work that way, right?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Ok the issue here seems to be that count doesn't appear to change or is not not 0. If I wiped out the do/while(empty), and checked if preQ.retrieve(qItem.element)==underflow it showed that the result was indeed underflow. Meaning the problem comes back to count.
    Why doesn't it change?..

    or the other option (which would seem to explain everything)

    is that something is not getting passed correctly... And either things are being destroyed out of scope that I don't want to be destroyed or pointers are pointing to garbage.

    Though I thought the way I have it now was passing by reference so that I wouldn't have such problems ?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    bump still looking for the answer here... added the header files I forgot to put in earlier.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You probably want to pass preQ as a reference.
    Code:
    void input (Queue preQ)
    {
    	string expression;
    	int i;
    
    	cout<<"Enter a prefix expression: ";
    	cin>>expression;
    
    	for (i=0; i<=expression.length(); ++i)
    	{
    		preQ.append(expression[i]);
    	}
    }
    Code:
    void input (Queue &preQ)
    {
    	string expression;
    	int i;
    
    	cout<<"Enter a prefix expression: ";
    	cin>>expression;
    
    	for (i=0; i<=expression.length(); ++i)
    	{
    		preQ.append(expression[i]);
    	}
    }
    Woop?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by prog-bman View Post
    You probably want to pass preQ as a reference.
    Code:
    void input (Queue preQ)
    {
    	string expression;
    	int i;
    
    	cout<<"Enter a prefix expression: ";
    	cin>>expression;
    
    	for (i=0; i<=expression.length(); ++i)
    	{
    		preQ.append(expression[i]);
    	}
    }
    Code:
    void input (Queue &preQ)
    {
    	string expression;
    	int i;
    
    	cout<<"Enter a prefix expression: ";
    	cin>>expression;
    
    	for (i=0; i<=expression.length(); ++i)
    	{
    		preQ.append(expression[i]);
    	}
    }
    I thought so too, and my compiler wouldn't hear of it... So I tried googling passing class objects by reference and just found a lot of ambiguous stuff... So I gave up on that idea, but I tried it again today and I guess today the compiler was in a better mood so it let me do that....

    Once I did that I got an inexplicable crash error so I turned to debugging and found it was a SEGFAULT with the following code:

    Code:
    Error_code Stack::push(const Stack_entry &item)
    {
    	Error_code outcome = success;
    	if (myTop >= MAXSTACK-1)
    		outcome = overflow;
    	else
    		entry[myTop++] = item;
    	return outcome;
    }
    particularly the part I bolded. Apparently VC++ doesn't like trying to increment myTop while also using that value for its entry. Or something. So I changed it to be more explicitly:
    Code:
    Error_code Stack::push(const Stack_entry &item)
    {
    	Error_code outcome = success;
    	if (myTop >= MAXSTACK-1)
    		outcome = overflow;
    	else
    	{
    		myTop++;
    		entry[myTop] = item;
    	}
    	return outcome;
    }
    Tada, it was all fine with that.

    I'm still not done, but now I'm actually getting something and its down to the logic of the pre_to_post code.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sparrowhawk
    Apparently VC++ doesn't like trying to increment myTop while also using that value for its entry. Or something.
    You are using post-increment, so it results in entry[-1] = item when myTop == -1. You could use pre-increment if you really wanted, i.e., entry[++myTop] = item.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    You are using post-increment, so it results in entry[-1] = item when myTop == -1. You could use pre-increment if you really wanted, i.e., entry[++myTop] = item.
    True enough. It's quite early where I am, just woke up so I'm not of completely sound mind yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Garbage Collector, Managed Heap etc.
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 08-07-2008, 12:42 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  4. garbage collection
    By joed in forum C Programming
    Replies: 4
    Last Post: 04-01-2004, 01:47 PM
  5. Array[] garbage output
    By guda in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2002, 12:52 PM