Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   stack question (http://www.programmingforums.org/showthread.php?t=9694)

bl00dninja May 8th, 2006 1:05 AM

stack question
 
am i creating a dangling pointer by NOT deleting the array associated with the stackClassDriver objects i'm making?

note: MaxStack is a constant int declared globally (for debugging and shit)

here is the class declaration:
:

template< class stackType, int MaxStack >
  class Stack
  {
        private:
                stackType stackItems[ MaxStack ];
                int top;

        public:
                Stack()                { top = -1; }//set top to reside right before the first element when empty
                void push( stackType );
                void pop();
                void print();
  };


here is the definition:
:

//push an element onto the stack
template< class stackType, int MaxStack >
  void Stack< stackType, MaxStack >::push(stackType pushVal)
  {
          //make sure stack is not full
          if (top < MaxStack - 1)
          {
              stackItems[++top] = pushVal;//increment top then add stackItem
                  cout<<"\n\n"<<endl;
          }
          else
                  cout<<"Error, stack bound exceeded (greater than 100 items in stack)\n\n"<<endl;
  }

  //pop an stackItem off of the stack and display what was just popped
  template< class stackType, int MaxStack >
  void Stack< stackType, MaxStack >::pop()
  {
          cout<<"\n\n\n\n"<<endl;
          if (top > -1)
          {
                  cout<<"\""<<stackItems[top--]<<"\" was popped off the stack\n\n"<<endl;//display then pop
          }
          else
          {
                  cout<<"Error, bottom of stack reached\n\n"<<endl;
          }
  }

  //print elements in the stack from top to bottom
  template< class stackType, int MaxStack >
  void Stack< stackType, MaxStack >::print()
  {
          //temp var to keep from changing the stack just to print
          cout<<"\n\n\n\n"<<endl;
          int printerPointer = top;
          while (printerPointer > -1)
          {
                  cout<<stackItems[printerPointer]<<endl;
                  printerPointer--;
          }
          cout<<"\n\n\n\n"<<endl;
  }


here is the driver:
note that it repeats for whatever class type they want to make
:

int main()
{
        //Stack<int, MaxStack>* stackClassDriver;
        //int stackItem;
        while (1)//begin main while
        {
                int mainMenuChoice;  //variable for stack type

                cout<<"which stack would you like to create?"<<endl;
                cout<<"1:  int\t2:  long\n3:  float\t4:  char"<<endl;
                cout<<"5:  quit"<<endl;
                cout<<"(choose 1-5)\n\n"<<endl;

                cin>>mainMenuChoice;               

                if (mainMenuChoice == 1)//begin int stack stackClassDriver
                {
                        Stack<int, MaxStack>* stackClassDriver = new Stack<int, MaxStack>;
                        while (1)
                        {
                                int stackItem;
                                int stackOperation;

                                cout<<"1:\tpush\t2:\tpop"<<endl;
                                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                                cout<<"(choose 1-4)\n\n"<<endl;
                                cin>>stackOperation;

                                if(stackOperation ==1)
                                {   
                                        cout << "Enter stackItem to push: ";
                                        cin >> stackItem;
   
                                        stackClassDriver->push(stackItem);
                                }

                                else if(stackOperation == 2)
                                {
                                        stackClassDriver->pop();
                                }

                                else if(stackOperation == 3)
                                {
                                        stackClassDriver->print();
                                }

                                else if(stackOperation == 4)
                                {
                                        delete stackClassDriver;
                                        stackClassDriver = 0;
                                        break;
                                }

                                else
                                {
                                        cout<<"invalid selection...\n\n"<<endl;
                                        continue;
                                }
                        }//end while
                }//end int stack stackClassDriver

                else if (mainMenuChoice == 2)//begin long stack stackClassDriver
                {
                        Stack<long, MaxStack>* stackClassDriver = new Stack<long, MaxStack>;
                        while (1)
                        {
                                long stackItem;
                                int stackOperation;

                                cout<<"1:\tpush\t2:\tpop"<<endl;
                                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                                cout<<"(choose 1-4)\n\n"<<endl;
                                cin>>stackOperation;

                                if(stackOperation ==1)
                                {   
                                        cout << "Enter stackItem to push: ";
                                        cin >> stackItem;
   
                                        stackClassDriver->push(stackItem);
                                }

                                else if(stackOperation == 2)
                                {
                                        stackClassDriver->pop();
                                }

                                else if(stackOperation == 3)
                                {
                                        stackClassDriver->print();
                                }

                                else if(stackOperation == 4)
                                {
                                        delete stackClassDriver;
                                        stackClassDriver = 0;
                                        break;
                                }

                                else
                                {
                                        cout<<"invalid selection...\n\n"<<endl;
                                        continue;
                                }
                        }//end while
                }//end long stack stackClassDriver

                else if (mainMenuChoice ==3)//begin float stack stackClassDriver
                {
                        Stack<float, MaxStack>* stackClassDriver = new Stack<float, MaxStack>;
                        while (1)
                        {
                                float stackItem;
                                int stackOperation;

                                cout<<"1:\tpush\t2:\tpop"<<endl;
                                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                                cout<<"(choose 1-4)\n\n"<<endl;
                                cin>>stackOperation;

                                if(stackOperation ==1)
                                {   
                                        cout << "Enter stackItem to push: ";
                                        cin >> stackItem;
   
                                        stackClassDriver->push(stackItem);
                                }

                                else if(stackOperation == 2)
                                {
                                        stackClassDriver->pop();
                                }

                                else if(stackOperation == 3)
                                {
                                        stackClassDriver->print();
                                }

                                else if(stackOperation == 4)
                                {
                                        delete stackClassDriver;
                                        stackClassDriver = 0;
                                        break;
                                }

                                else
                                        cout<<"invalid selection...\n\n<<endl";
                                        continue;
                        }//end while
                }//end float stack stackClassDriver

                else if (mainMenuChoice == 4)//begin char stack stackClassDriver
                {
                        Stack<char, MaxStack>* stackClassDriver = new Stack<char, MaxStack>;
                        while (1)
                        {
                                char stackItem;
                                int stackOperation;

                                cout<<"1:\tpush\t2:\tpop"<<endl;
                                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                                cout<<"(choose 1-4)\n\n"<<endl;
                                cin>>stackOperation;

                                if(stackOperation ==1)
                                {   
                                        cout << "Enter stackItem to push: ";
                                        cin >> stackItem;
   
                                        stackClassDriver->push(stackItem);
                                }

                                else if(stackOperation == 2)
                                {
                                        stackClassDriver->pop();
                                }

                                else if(stackOperation == 3)
                                {
                                        stackClassDriver->print();
                                }

                                else if(stackOperation == 4)
                                {
                                        delete stackClassDriver;
                                        stackClassDriver = 0;
                                        break;
                                }

                                else
                                        cout<<"invalid selection...\n\n<<endl";
                                        continue;
                        }//end while
                }//end char stack stackClassDriver*/

                else if (mainMenuChoice == 5)//quit app
                {
                        break;
                }

                else//detect invalid mainMenuChoice
                {
                        cout<<"invalid selection...\n\n"<<endl;
                        continue;
            }
        }//end main while

    return 0;
} //end main


bl00dninja May 8th, 2006 1:08 AM

oh, and any help with making the driver shorter would be welcomed. i tried to use a switch to instantiate the objects before running the choices and stuff, but ran into problems. i thought about polymorphism, but how does that work with templates?

Jimbo May 8th, 2006 1:55 AM

I'm going to take a guess and say that you won't have any memory leaks. Since MaxStack is constant, it should be akin to just declaring a static array as a member of the Stack class. Have you tried running it through a debugger or something that watches your memory to see if you get any leaks? IIRC, VC++ will give some notice of dumping unfreed memory when the debugger finishes.

InfoGeek May 8th, 2006 6:52 AM

You can make your driver shorter by doing something like:
:

while (1)//begin main while
        {
                int mainMenuChoice;  //variable for stack type

                cout<<"which stack would you like to create?"<<endl;
                cout<<"1:  int\t2:  long\n3:  float\t4:  char"<<endl;
                cout<<"5:  quit"<<endl;
                cout<<"(choose 1-5)\n\n"<<endl;

                cin>>mainMenuChoice;               
                switch(mainMenuChoice)
                {
                case 1:
                        Stack<int, MaxStack>* stackClassDriver = new Stack<int, MaxStack>;
                        int stackItem;
                        break;
                case 2:
                        Stack<long, MaxStack>* stackClassDriver = new Stack<long, MaxStack>;
                        long stackItem;
                        break;
                case 3:
                        Stack<float, MaxStack>* stackClassDriver = new Stack<float, MaxStack>;
                        float stackItem;
                        break;
                case 4:
                        Stack<char, MaxStack>* stackClassDriver = new Stack<char, MaxStack>;
                        char stackItem;
                        break;
                case 5:
                        return 0;
                default:
                        cout<<"invalid selection...\n\n"<<endl;
                        continue;
                }
        done=false;
        while (!done)
        {
                int stackOperation;

                cout<<"1:\tpush\t2:\tpop"<<endl;
                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                cout<<"(choose 1-4)\n\n"<<endl;
                cin>>stackOperation;

                switch(stackOperation)
                {
                case 1:
                        cout << "Enter stackItem to push: ";
                        cin >> stackItem;
                        stackClassDriver->push(stackItem);
                        break;
                case 2:
                        stackClassDriver->pop();
                        break;
                case 3:
                        stackClassDriver->print();
                        break;

                case 4:
                        delete stackClassDriver;
                        stackClassDriver = 0;
                        done=true;
                        break;
                default:
                        cout<<"invalid selection...\n\n"<<endl;
                        continue;
                }
        }//end while
}


The Dark May 8th, 2006 10:47 AM

Quote:

Originally Posted by InfoGeek
You can make your driver shorter by doing something like:
:

                switch(mainMenuChoice)
                {
                case 1:
                        Stack<int, MaxStack>* stackClassDriver = new Stack<int, MaxStack>;
                        int stackItem;
                        break;
                ...


You can't declare something inside a switch statement and access it outside of the switch statement, it would then be out of scope.

To make the code shorter, you could move the repeated stuff into a template function.

bl00dninja May 8th, 2006 11:07 AM

ok, yeah i wasn't getting any problems with the debugger, and yeah THAT'S the problem i was having with the driver. thanks!

any other thoughts?

InfoGeek May 8th, 2006 11:49 AM

My bad, I should think before I post :o
May be something like this should work:
:

int StackItem1;
long StackItem2;
float StackItem3;
char StackItem4;
while (1)//begin main while
        {
                int mainMenuChoice;  //variable for stack type

                cout<<"which stack would you like to create?"<<endl;
                cout<<"1:  int\t2:  long\n3:  float\t4:  char"<<endl;
                cout<<"5:  quit"<<endl;
                cout<<"(choose 1-5)\n\n"<<endl;

                cin>>mainMenuChoice;               
                switch(mainMenuChoice)
                {
                case 1:
                        Stack<int, MaxStack>* stackClassDriver = new Stack<int, MaxStack>;
                        break;
                case 2:
                        Stack<long, MaxStack>* stackClassDriver = new Stack<long, MaxStack>;
                        break;
                case 3:
                        Stack<float, MaxStack>* stackClassDriver = new Stack<float, MaxStack>;
                        break;
                case 4:
                        Stack<char, MaxStack>* stackClassDriver = new Stack<char, MaxStack>;
                        break;
                case 5:
                        return 0;
                default:
                        cout<<"invalid selection...\n\n"<<endl;
                        continue;
                }
        done=false;
        while (!done)
        {
                int stackOperation;

                cout<<"1:\tpush\t2:\tpop"<<endl;
                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                cout<<"(choose 1-4)\n\n"<<endl;
                cin>>stackOperation;

                switch(stackOperation)
                {
                case 1:
                        cout << "Enter stackItem to push: ";
                        cin >> stackItem;
                        switch(mainMenuChoice)
                        {
                          case 1:
                              stackClassDriver->push(StackItem1);
                              break;
                          case 2:
                              stackClassDriver->push(StackItem2);
                              break;

                          case 3:
                              stackClassDriver->push(StackItem3);
                              break;

                          case 4:
                              stackClassDriver->push(StackItem4);
                              break;
                        }
                case 2:
                        stackClassDriver->pop();
                        break;
                case 3:
                        stackClassDriver->print();
                        break;

                case 4:
                        delete stackClassDriver;
                        stackClassDriver = 0;
                        done=true;
                        break;
                default:
                        cout<<"invalid selection...\n\n"<<endl;
                        continue;
                }
        }//end while
}

Edit: The Dark's suggestion is still better.

Cache May 8th, 2006 1:29 PM

Quote:

Originally Posted by InfoGeek
May be something like this should work:
...

Nope. The variable 'stackClassDriver' is still only defined within the scope of the switch statement. Pointer variables are subject to scope, too. Even if the object they point to isn't.

InfoGeek May 8th, 2006 7:28 PM

My apologies. I should hang myself :o

The Dark May 8th, 2006 7:54 PM

To make it shorter, put the repeated code into a template function:
:

template <class stackType, int MaxStack> void driverLoop()
{
  Stack<stackType, MaxStack>* stackClassDriver = new Stack<stackType, MaxStack>;
        while (1)
        {
                stackType stackItem;
                int stackOperation;

                cout<<"1:\tpush\t2:\tpop"<<endl;
                cout<<"3:\tprint\t4:\tmain menu"<<endl;
                cout<<"(choose 1-4)\n\n"<<endl;
                cin>>stackOperation;

                if(stackOperation ==1)
                {   
                        cout << "Enter stackItem to push: ";
                        cin >> stackItem;

                        stackClassDriver->push(stackItem);
                }

                else if(stackOperation == 2)
                {
                        stackClassDriver->pop();
                }

                else if(stackOperation == 3)
                {
                        stackClassDriver->print();
                }

                else if(stackOperation == 4)
                {
                        delete stackClassDriver;
                        stackClassDriver = 0;
                        break;
                }

                else
                {
                        cout<<"invalid selection...\n\n"<<endl;
                        continue;
                }
        }//end while
}

Then you can call it in your if statements like this:
:

                if (mainMenuChoice == 1)//begin int stack stackClassDriver
                {
                  driverLoop<int, MaxStack>();
                }//end int stack stackClassDriver

                else if (mainMenuChoice == 2)//begin long stack stackClassDriver
                {
                  driverLoop<long, MaxStack>();
                }//end long stack stackClassDriver



All times are GMT -5. The time now is 9:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC