Question about a stack using array of pointers

This is a discussion on Question about a stack using array of pointers within the C++ Programming forums, part of the General Programming Boards category; Well, I had an assignment for class to convert an array of objects stack to an array of pointers stack. ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    22

    Question about a stack using array of pointers

    Well, I had an assignment for class to convert an array of objects stack to an array of pointers stack. I think that I've oversimplified the assignment, because the program compiles okay, but whenever I push an item onto the stack, the program crashes. Using debug, it looks like it was assigning a value of 5 to my pointer, which is the max # of items in my array. I can't figure out why this is. Also, this is a class, which I'm not exactly comfortable with yet.

    Scroll to the bottom if you want to see the actual problem without the long explanation. Sorry for the backwards post.

    This is my class:

    Code:
    #ifndef STACKTYPE_H
    #define STACKTYPE_H
    #include "ItemType.h"
    
    //   The user of this file must provide a file "ItemType.h" that defines:
    //       ItemType : the class definition of the objects on the stack.
    //       MAX_ITEMS: the maximum number of items on the stack. 
    
    //   Class specification for Stack ADT in file Stack1.h
    
    
    class FullStack
    // Exception class thrown by Push when stack is full.
    {};
    
    class EmptyStack
    // Exception class thrown by Pop and Top when stack is emtpy.
    {};
    
    
    class StackType
    {
    public:
      StackType(int);                 // added 10/9/2003
      StackType();
      // Class constructor.
      bool IsFull() const;
      // Function: Determines whether the stack is full.
      // Pre:  Stack has been initialized.
      // Post: Function value = (stack is full)
      bool IsEmpty() const;
      // Function: Determines whether the stack is empty.
      // Pre:  Stack has been initialized.
      // Post: Function value = (stack is empty)
      void Push( const ItemType & item);
      // Function: Adds newItem to the top of the stack.
      // Pre:  Stack has been initialized.
      // Post: If (stack is full), FullStack exception is thrown;
      //     otherwise, newItem is at the top of the stack.
      void Pop();
      // Function: Removes top item from the stack.
      // Pre:  Stack has been initialized.
      // Post: If (stack is empty), EmptyStack exception is thrown;
      //     otherwise, top element has been removed from stack.
      ItemType Top();
      // Function: Returns a copy of top item on the stack.
      // Pre:  Stack has been initialized.
      // Post: If (stack is empty), EmptyStack exception is thrown;
      //     otherwise, top element has been removed from stack.
    
           
    private:
      int top;
      ItemType ** items;		//  changed 10/9/2003
    };
    #endif
    That part is fine I think.

    This is ItemType if you're curious:

    Code:
    // ItemType.h StackDriver
    #ifndef ITEMTYPE_H
    #define ITEMTYPE_H
    const int MAX_ITEMS = 5;
    typedef int ItemType;
    
    #endif
    This is my driver program:

    Code:
    // Test driver
    #include <iostream>
    #include <fstream>
    
    #include "StackType.h"
    
    using namespace std;
    bool copyStack( StackType & d, StackType & s)
    {
    	StackType temp;
    	while ( !s.IsEmpty() )
    	{
    	     temp.Push(	   s.Top()  );
    		 s.Pop();
    	}
    	return true;
    }
    
    int main()
    {
      StackType s;
      ItemType work;
      while ( ! s.IsFull())
      { 
    	  cin >> work;
    	  s.Push(work);
      }
      //  empty the stack - printing the data
      while ( !s.IsEmpty() )
      {
    	  work = s.Top();
    	  cout << work;
    	  s.Pop();
      }
    
    
    
      return 0;
    }
    I believe that is fine as well. I don't think it should have to change?

    Finally, this is my stack:

    Code:
    / File: StackType.cpp
    
    #include "StackType.h"
    #include <iostream>
    ItemType dummy;
    
    StackType::StackType()
    {
      top = -1;
    }
    
    bool StackType::IsEmpty() const
    {
      return (top == -1);
    }
    
    bool StackType::IsFull() const
    {
      return (top ==  MAX_ITEMS-1);
    }
    
    void StackType::Push(const ItemType & newItem)
    {
      if( IsFull() )
        throw FullStack();
      top++;
      *items[top] = newItem;
    }
    
    void StackType::Pop()
    {
      if( IsEmpty() )
        throw EmptyStack();
      *items[top] = dummy;
      top--;
    }
    
    ItemType StackType::Top()
    {
      if (IsEmpty())
        throw EmptyStack();
      return *items[top];
    }
    I'm quite sure this is where my problem lies. I think that I can't just do this:

    *items[top] = newItem;

    as that seems to be where the problem occurs. How would you go about pushing the value onto the stack? I don't really understand how pointers change this.

    Also, sorry for posting so much code. But last time I didn't post enough, so I thought I would avoid that problem this time.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Because items[top] doesn't point at anything. You are actually trying to copy one object to another using the assignment operator.

    What is push supposed to do? Create a new item and copy the contents of the parameter, or is it supposed to simply take a pointer to the original item (which could be dangerous if the original item vanishes and you still have the pointer to a dead object).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm... is against the rules to use a std::list? I believe that would add a certain amount of versatility to your stack, an example being unlimited stack size until the computer runs out of memory, and another example being the code is easier to write.

    Also, I don't see why you're passing a const int& to Push(). By my line of reasoning, if you're making it a stack of pointers, you probably want to pass either a pointer or a pointer-pointer

    **EDIT**
    Also, no idea why you'd really want to throw an exception when you could just return an error code; it's not like an error code would interfere with a normal return value or anything, since the current return type is void.
    Last edited by Hunter2; 11-17-2003 at 09:45 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Hmm...I believe I saw this exact code in some book not too long ago. I remember as this book used the throw of an empty and full stack. Are you sure this is your code?

    I actaully downloaded some of the source files from the book's website and indeed...it is the same code with exactly same comments.

    I hope that you will give credit to the original author of the code, as none of it is your own work.

    edit:: I did some further research, and the books is C++ Plus Data Structures by Nell Dale. If someone as random as me can recognize the code so fast make sure your professor doesn't do the same. Professors often look at a bunch of books before each semester to pick the one they like most. And you really will not learn much by copying someone elses code my friend.
    Last edited by axon; 11-17-2003 at 09:49 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    It is the code from that book. That's the book we use in class, and as I mentioned our assignment was to modify that code for an array using pointers. Sorry, maybe I should have been more clear about this.

    I didn't think I should use a const, but when I tried to change it to a pointer, I got a build error, so I changed it back. Sorry, I forgot about that.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I am sorry if indeed this has been the code provided by your professor. I wonder why your professor would use that book thought; the explanations in it are very dire. Some chapters are mostly theory and no code, and some are just code and no explanations....

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    Yeah, I've noticed. I don't know if he chose it, or if they have the same text book for all the CS250 classes. But he complains about it a lot as well. Though the driver program was the only one we really redid on this assignment. I think maybe I should just ask him about this, because I fail to see the reasoning behind the assignment or how to do it obviously.
    Last edited by Ricochet; 11-17-2003 at 10:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 07:30 PM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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