![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 882
Rep Power: 4
![]() |
Quote:
To make the code shorter, you could move the repeated stuff into a template function. |
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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
}
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#8 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
My apologies. I should hang myself :o
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#10 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 882
Rep Power: 4
![]() |
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
} 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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|