Memory Garbage - Please Help!! Desperate! :(

This is a discussion on Memory Garbage - Please Help!! Desperate! :( within the C++ Programming forums, part of the General Programming Boards category; Hi, I am working on this assignment and I have tried EVERYTHING, but to no avail. I'm working on this ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    Unhappy Memory Garbage - Please Help!! Desperate! :(

    Hi,

    I am working on this assignment and I have tried EVERYTHING, but to no avail.

    I'm working on this stack and queue fusion called quack and the memory is driving me CRAZY! In the output, it displays actual numbers for pushFront and pushBack. But for popFront and popBack functions, it displays garbage.

    What I am trying to do is to fix that garbage so that it can be a real number. I have tried declaring the memory and nothing happened. My brother tried to help and we spent hours on it. Even tutors, who are SUPPOSED to be helpful have no clue. I am completely out of ideas!

    I just need someone to fix this for me so that I can work on the rotation and reverse functions next, because I don't want to work on this again. I don't want hints! Just answers. Any help is appreciated. Thank you.

    Here are the codes:

    quack.h (the header file for quack) - The only thing I added under the private section were front, back, count, and capacity.
    Code:
    // you can only change this file by adding items to the "private" section
    
    #pragma once
    
    #include <ostream>
    
    using namespace std;
    
    class quack
    {
    public:
    	quack(int capacity);
    	~quack(void);
    	bool pushFront(const int n);	// push an item onto the front
    	bool pushBack(const int n);		// push an item onto the back
    	bool popFront(int& n);			// pop an item off the front
    	bool popBack(int& n);			// pop an item off the back
    	void rotate(int r);				// "rotate" the stored items (see note below)
    	void reverse(void);				// reverse the order of the stored items
    	int	itemCount(void);			// return the current number of stored items
    
    private:
    	struct item						// definition of each item stored by the quack
    	{
    		int		n;
    	};
    
    	item		      *items;				// pointer to storage for the circular array
    	int			front;				// first item in the quack
    	int			back;				// last item in the quack
    	int			count;				// Counts how many items in the quack
    	int			capacity;			// The max number of the quack
    
    public:
    	friend ostream& operator<<(ostream& out, quack& q);
    	friend ostream& operator<<(ostream& out, quack::item& i);
    };
    
    // the following example should make it clear what "rotate" means:
    //   quack: 2, 1, 0, 7, 8		-- current contents, front is on the left
    //   >>> rotate(2)
    //   quack: 0, 7, 8, 2, 1		-- contents after rotation by +2
    //   >>> rotate(-3)
    //   quack: 8, 2, 1, 0, 7		-- contents after rotation by -3
    quack.cpp (the code I am working on) - again I haven't done the rotation and reverse functions yet.
    Code:
    // add code to these functions to make quack do something useful
    
    // enable Visual C++ memory leak checking
    #ifdef _DEBUG
    #include <ostream>
    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif
    
    #include "quack.h"
    
    /*********************
    private:
    item		      *items;		// pointer to storage for the circular array
    int			front;	        // index of the first item in the quack
    int			back;	        // index of the last item in the quack
    int			count;	        // Counts how many items in the quack
    const int	        capacity;		// The max number of the quack
    *********************/
    
    quack::quack(int capacity) : 
    	capacity(capacity), 
    	front(capacity - 1), 
    	back(capacity), 
    	count(0), 
    	items(new item[capacity])
    {
    }
    
    quack::~quack(void)
    {
    	delete [] items;
    }
    
    bool quack::pushFront(const int n)
    {	
    
    	if (count == capacity)
    		return false;
    	else
    		front = (front - 1) % capacity;
    		items[front].n = n;
    		count++;
    		return true;
    }
    
    bool quack::pushBack(const int n)
    {
    
    	if (count == capacity)
    		return false;
    	else
    		back = (back = front + count) % capacity;
    		items[back].n = n;
    		count++;
    		return true;
    }
    
    bool quack::popFront(int& n)
    {
    	front = (front + 1) % capacity;
    	--count;
    	return true;
    }
    
    bool quack::popBack(int& n)
    {
    	back = (back - 1) % capacity;
    	--count;
    	return true;
    }
    
    void quack::rotate(int r)
    {
    }
    
    void quack::reverse(void)
    {
    }
    
    int	quack::itemCount(void)
    {
    	return count;
    }
    
    ostream& operator<<(ostream& out, quack& q)
    {
    	// alter this function as needed to produce the required output
    
      if ( q.count == 0 ) // no elements have been counted.
        out << endl << "quack: empty" << endl;
        else
        {
            out << endl << "quack: ";
            for ( int i = 1; i < q.count + 1; i++ )
            {
                   
            out << q.items[i] << ", ";
                   
            }
            out << endl << endl;
        }
        return out;
    
    }
    
    ostream& operator<<(ostream& out, quack::item& i)
    {
    out << i.n;
    	return out;
    }
    Here's the driver code to run the program. It's readable-only
    Code:
    // make no changes to this file except to have it
    // print your own name instead of "Your Name"
    
    // enable Visual C++ memory leak checking
    #ifdef _DEBUG
    #include <ostream>
    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif
    
    #include <iostream>
    #include "quack.h"
    
    using namespace std;
    
    const static int QUACK_SIZE = 7;
    
    static void push(quack& q, bool front, int n)
    {
    	bool	ok;
    
    	if (front)
    		ok = q.pushFront(n);
    	else
    		ok = q.pushBack(n);
    	cout << ">>> push" << (front ? "Front " : "Back ") << n << (ok ? " succeeded\n" : " failed\n");
    }
    
    static void pop(quack& q, bool front)
    {
    	bool	ok;
    	int		n;
    
    	if (front)
    		ok = q.popFront(n);
    	else
    		ok = q.popBack(n);
    	cout << ">>> pop" << (front ? "Front " : "Back ");
    	if (ok)
    		cout << "succeeded: " << n << endl;
    	else
    		cout << "failed\n";
    }
    
    int main (void)
    {
    	quack	q(QUACK_SIZE);
    
    #ifdef _WIN32
    	// request memory leak report in Output Window after main returns
    	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    #endif
    
    	cout << "CS260 - Lab2 - Christian Rhodes\n";
    	cout << q;
    	push(q, true, 1);
    	push(q, true, 2);
    	push(q, true, 3);
    	push(q, true, 4);
    	push(q, false, 0);
    	push(q, true, 9);
    	cout << q;
    	cout << "--- # of items: " << q.itemCount() << endl << endl;
    	pop(q, true);
    	cout << q;
    	pop(q, true);
    	cout << q;
    	push(q, false, 7);
    	cout << q;
    	push(q, false, 8);
    	cout << q;
    	cout << ">>> rotate(2)\n";
    	q.rotate(2);
    	cout << q;
    	cout << ">>> rotate(-3)\n";
    	q.rotate(-3);
    	cout << q;
    	cout << ">>> reverse\n";
    	q.reverse();
    	cout << q;
    	push(q, true, 6);
    	cout << q;
    	cout << ">>> rotate(3)\n";
    	q.rotate(3);
    	cout << q;
    	cout << ">>> rotate(-4)\n";
    	q.rotate(-4);
    	cout << q;
    	cout << "--- # of items: " << q.itemCount() << endl << endl;
    	while (q.itemCount() > 0) {
    		pop(q, false);
    		cout << q;
    		}
    	cout << "--- # of items: " << q.itemCount() << endl << endl;
    
    	return 0;
    }
    Here's the output. This is what the output suppose to show
    Code:
    CS260 - Lab2 - Your Name
    
    quack: empty
    
    >>> pushFront 1 succeeded
    >>> pushFront 2 succeeded
    >>> pushFront 3 succeeded
    >>> pushFront 4 succeeded
    >>> pushBack 0 succeeded
    >>> pushFront 9 succeeded
    
    quack: 9, 4, 3, 2, 1, 0
    
    --- # of items: 6
    
    >>> popFront succeeded: 9
    
    quack: 4, 3, 2, 1, 0
    
    >>> popFront succeeded: 4
    
    quack: 3, 2, 1, 0
    
    >>> pushBack 7 succeeded
    
    quack: 3, 2, 1, 0, 7
    
    >>> pushBack 8 succeeded
    
    quack: 3, 2, 1, 0, 7, 8
    
    >>> rotate(2)
    
    quack: 1, 0, 7, 8, 3, 2
    
    >>> rotate(-3)
    
    quack: 8, 3, 2, 1, 0, 7
    
    >>> reverse
    
    quack: 7, 0, 1, 2, 3, 8
    
    >>> pushFront 6 succeeded
    
    quack: 6, 7, 0, 1, 2, 3, 8
    
    >>> rotate(3)
    
    quack: 1, 2, 3, 8, 6, 7, 0
    
    >>> rotate(-4)
    
    quack: 8, 6, 7, 0, 1, 2, 3
    
    --- # of items: 7
    
    >>> popBack succeeded: 3
    
    quack: 8, 6, 7, 0, 1, 2
    
    >>> popBack succeeded: 2
    
    quack: 8, 6, 7, 0, 1
    
    >>> popBack succeeded: 1
    
    quack: 8, 6, 7, 0
    
    >>> popBack succeeded: 0
    
    quack: 8, 6, 7
    
    >>> popBack succeeded: 7
    
    quack: 8, 6
    
    >>> popBack succeeded: 6
    
    quack: 8
    
    >>> popBack succeeded: 8
    
    quack: empty
    
    --- # of items: 0
    But, this is what I'm getting in the output instead of the above output.
    Code:
    CS260 - Lab2 - Christian Rhodes
    
    quack: empty
    >>> pushFront 1 succeeded
    >>> pushFront 2 succeeded
    >>> pushFront 3 succeeded
    >>> pushFront 4 succeeded
    >>> pushBack 0 succeeded
    >>> pushFront 9 succeeded
    
    quack: 9, 4, 3, 2, 1, 0,
    
    --- # of items: 6
    
    >>> popFront succeeded: -858993460
    
    quack: 9, 4, 3, 2, 1,
    
    >>> popFront succeeded: -858993460
    
    quack: 9, 4, 3, 2,
    
    >>> pushBack 7 succeeded
    
    quack: 9, 4, 3, 2, 1,
    
    >>> pushBack 8 succeeded
    
    quack: 8, 4, 3, 2, 1, 0,
    
    >>> rotate(2)
    
    quack: 8, 4, 3, 2, 1, 0,
    
    >>> rotate(-3)
    
    quack: 8, 4, 3, 2, 1, 0,
    
    >>> reverse
    
    quack: 8, 4, 3, 2, 1, 0,
    
    >>> pushFront 6 succeeded
    
    quack: 8, 6, 3, 2, 1, 0, -33686019,
    
    >>> rotate(3)
    
    quack: 8, 6, 3, 2, 1, 0, -33686019,
    
    >>> rotate(-4)
    
    quack: 8, 6, 3, 2, 1, 0, -33686019,
    
    --- # of items: 7
    
    >>> popBack succeeded: -858993460
    
    quack: 8, 6, 3, 2, 1, 0,
    
    >>> popBack succeeded: -858993460
    
    quack: 8, 6, 3, 2, 1,
    
    >>> popBack succeeded: -858993460
    
    quack: 8, 6, 3, 2,
    
    >>> popBack succeeded: -858993460
    
    quack: 8, 6, 3,
    
    >>> popBack succeeded: -858993460
    
    quack: 8, 6,
    
    >>> popBack succeeded: -858993460
    
    quack: 8,
    
    >>> popBack succeeded: -858993460
    
    quack: empty
    --- # of items: 0
    Again, any help is greatly appreciated. Thank you.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    So, um.
    Code:
    bool quack::popFront(int& n)
    {
    	front = (front + 1) % capacity;
    	--count;
    	return true;
    }
    Where are you setting n, the source of the "garbage", in this function?

    GIGO

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Not to go all George Carlin on ya but you have not tried *everything* because you know there is an answer out there that you have not tried yet so...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    5
    Quote Originally Posted by rags_to_riches View Post
    So, um.
    Code:
    bool quack::popFront(int& n)
    {
    	front = (front + 1) % capacity;
    	--count;
    	return true;
    }
    Where are you setting n, the source of the "garbage", in this function?

    GIGO

    The n is right here
    Code:
    struct item						// definition of each item stored by the quack
    	{
    		int		n;
    	};
    Quote Originally Posted by jeffcobb View Post
    Not to go all George Carlin on ya but you have not tried *everything* because you know there is an answer out there that you have not tried yet so...
    Like I said, I've ran out of ideas! I don't know what else to do! Maybe you can show me where the problem is, cuz I don't know...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by yapic View Post
    The n is right here
    Code:
    struct item						// definition of each item stored by the quack
    	{
    		int		n;
    	};
    That is a separate variable, you need to be doing something with the n inside the popFront function. What did you think the n in popFront was for - decoration?
    You need to copy from the n you just referred to, to the n rags_to_riches just mentioned.
    Hint, it's the reverse of what you're doing in the pushFront function. This shouldn't be much of a mystery to you if you wrote the pushFront function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I don't want hints! Just answers.
    OK...the answer is...42.42 (number) - Wikipedia, the free encyclopedia
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 05:17 AM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:41 PM
  3. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 04:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21