Template Stack and LIFO C++ -


so i'm trying learn templates , fifo , lifo stack stuff. i've been playing around code deals this, , can int data want testing can't life of me figure out how work string. way have code keeps crashing on me, doesn't give me errors, thought i'd pop in here , see if tell me i'm doing wrong. here's code:

-----------//my header//---------------------  #include <stdlib.h> #include <iostream> #include <string>  #ifndef stack_h_ #define stack_h_  template<class t>  class stacktest {  private: unsigned int maxsize; t *stackdata; int top;  public: stacktest(int size){     stackdata = new t[size];//to hold t ‌type data items      top = -1;//no items on stack     maxsize = size;//set maximum size stack can hold }  virtual ~stacktest(){}  int count(){     return top + 1; }  bool isempty(){     return top == -1 ? true : false; }  bool isfull(){     return top == maxsize - 1 ? true : false; }  t* peek(){     if(!isempty())//check empty         return &stackdata[top - 1]; }  t* pop(){     if(!isempty()){         top -= 1;//decrease top 1 indicate delete         return &stackdata[top];//return deleted item     }     return null; }  void push(t* item){     stackdata[top++] = *item;//insert data array , increase top 1  } };   #endif /* stack_h_ */  -----------//my main//---------------  #include <iostream> #include <string> #include "pair.h"  using namespace std;  int main() {  int datatest; string strtest; stacktest<int> intstack(10); stacktest<string> stringstack(50);  //insert data stack datatest = 3; intstack.push(&datatest); datatest = 4; intstack.push(&datatest); datatest = 5; intstack.push(&datatest); datatest = 6; intstack.push(&datatest); strtest = "test"; stringstack.push(&strtest);  //show top item cout << *intstack.peek() << endl; cout << *stringstack.peek() << endl;  //pull top item out (twice) intstack.pop(); intstack.pop();  //show new top item cout << *intstack.peek() << endl;  return 0; } 

so if feels giving me pointers appreciate it, thanks.

there few issues implementation. 1 of subtle in push() member function:

void push(t* item){     stackdata[top++] = *item; //insert data array , increase top 1     //           ^^     //           want pre-increment here! } 

this incrementing top , using old value index stackdata. since top -1 when stack empty, program doing:

stackdata[-1] = *item; top = 0; 

needless first assignment results in undefined behavior.

another source of undefined behavior peek() member function, not return when stack empty:

t* peek(){     if(!isempty())//check empty         return &stackdata[top - 1]; } 

per paragraph 6.6.3/2 of c++11 standard:

[...] flowing off end of function equivalent return no value; results in undefined behavior in value-returning function.

but that's not issue: other problem access of stackdata:

return &stackdata[top - 1]; 

when top not equal or greater one, result in undefined behavior, since taking address of (non-)object located @ negative address in array.

also, suggest rewrite isempty() , isfull() follows:

bool isempty(){     return (top == -1); }  bool isfull(){    return (top == maxsize - 1); } 

as general advice, consider not using value -1 top when stack empty. as ben voigt mentions in comments, leading lot of off-by-one errors.

also, as pointed out dyp, destructor not freeing memory allocated in constructor, stacktest object leaking memory. , after doing that, since we're @ it, may want have @ so-called rule of three, program violating.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -