C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-17-2003, 07:56 PM   #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.
Ricochet is offline   Reply With Quote
Old 11-17-2003, 09:13 PM   #2
Cat
Registered User
 
Join Date: May 2003
Posts: 1,185
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.
Cat is offline   Reply With Quote
Old 11-17-2003, 09:32 PM   #3
Carnivore ('-'v)
 
Hunter2's Avatar
 
Join Date: May 2002
Posts: 2,865
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.
__________________
Just Google It. √

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

Last edited by Hunter2; 11-17-2003 at 09:45 PM.
Hunter2 is offline   Reply With Quote
Old 11-17-2003, 09:40 PM   #4
Registered User
 
axon's Avatar
 
Join Date: Feb 2003
Location: Mt. Prospect, IL
Posts: 2,602
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.
__________________

some entropy with that sink? entropysink.com

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

Last edited by axon; 11-17-2003 at 09:49 PM.
axon is offline   Reply With Quote
Old 11-17-2003, 10:02 PM   #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.
Ricochet is offline   Reply With Quote
Old 11-17-2003, 10:07 PM   #6
Registered User
 
axon's Avatar
 
Join Date: Feb 2003
Location: Mt. Prospect, IL
Posts: 2,602
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
axon is offline   Reply With Quote
Old 11-17-2003, 10:12 PM   #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.
Ricochet is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting number Leslie C Programming 8 05-20-2009 04:23 AM
Vertical Scroller laser cannon problem Swarvy Game Programming 5 05-02-2009 06:30 PM
Dynamic array of structures containing yet another dynamic array of structures innqubus C Programming 2 07-11-2008 07:39 AM
Array of struct pointers - Losing my mind drucillica C Programming 5 11-12-2005 11:50 PM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM


All times are GMT -6. The time now is 02:55 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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