Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 8th, 2006, 2:05 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old May 8th, 2006, 2:08 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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?
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old May 8th, 2006, 2:55 AM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
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.
Jimbo is offline   Reply With Quote
Old May 8th, 2006, 7:52 AM   #4
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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
}
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 8th, 2006, 11:47 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 873
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old May 8th, 2006, 12:07 PM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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?
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old May 8th, 2006, 12:49 PM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 8th, 2006, 2:29 PM   #8
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 263
Rep Power: 4 Cache is on a distinguished road
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.
Cache is offline   Reply With Quote
Old May 8th, 2006, 8:28 PM   #9
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
My apologies. I should hang myself :o
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 8th, 2006, 8:54 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 873
Rep Power: 4 The Dark is on a distinguished road
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
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:45 AM.

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