Hello people,
I need help with a template program that i am writing .
I am trying to emulate Stack operations using template class which works fine with all the datatypes except char* when using all the datatypes at the same time.
What I am trying to do is pass an array of character strngs (char*) to the push().
I know that memory allocation is needed to store the strings on the stack which i find difficult to write because then I'll have to write the allocation code in the push function which I dont think is correct since the same function is used by other datatypes.
I dont get any error messages but am not able to get the proper output because the strings are not stored on the stack
However I am perfectly able to run the program when I am using just the char* datatype without any other datatypes (that's obvious). The program also works fine when I am using the string datatype.
My only objective is to use the char* with others at the same time.
So how can I do that or is it not possible?
Here's my code:

#include<iostream>
using namespace std;
template<class T> class Stack{
 public:
       Stack() : top(0){}
       void push(T ele);
       T pop();
  private: 
      int top;
       T array[10] ;
};

template<class T>T Stack<T>::pop(){
  assert(top > 0);
  return (array[--top]);
}

template<class T>void Stack<T>::push(T ele){
  assert(top < 10);
  array[top++] = ele;
}

int main(){
Stack<int> Si;
Si.push(2);
Si.push(3);

Stack<pop> Sf;
Sf.push(2);
Sf.push(3);

Stack<char*> Scptr;
char ar[] = "abhi";
char* ar2 = new char(strlen(ar));/*This certainly should not be here*/
Scptr.push(ar2);
cout<<Scptr.pop();//Doesn't print anything

return 0;
}

When I do this :

#include<iostream>
using namespace std;
template<class T> class Stack{
 public:
       Stack() : top(0){}
       void push(T  []);
       T pop();
  private: 
      int top;
       T array[10] ;
};

template<class T>T Stack<T>::pop(){
  assert(top > 0);
  return (array[--top]);
}

template<class T>void Stack<T>::push(T ele[]){
  assert(top < 10);
   array[top] = new char (strlen(ele)); //mem allocation 
  strcpy(array[top], ele);
  top++;
}

int main(){
Stack<char*> Scptr;
char ar[] = "abhi";
 Scptr.push(ar);
cout<<"Popped : "<< Scptr.pop(); //can see the output!
}

works perfect!
@I am using a Dev C++ 4.9.9.2 compiler@

>Stack<pop> Sf;
What do you expect pop to be?

>char* ar2 = new char(strlen(ar));
>Scptr.push(ar2);
You're allocating memory but not filling it. Try this instead:

Stack<char*> Scptr;
char ar[] = "abhi";
char* ar2 = new char(std::strlen(ar));
std::strcpy ( ar2, ar );
Scptr.push(ar);
cout<<Scptr.pop();

And be sure to include <cstring>.

Thanks Narue.
Will the Stack "array" take "ar" without any problems from push(ar)?

>Will the Stack "array" take "ar" without any problems from push(ar)?
No, but it creates an aliasing problem where if you change ar, the data in the stack will change too. It's better to make a copy of the string for storage in the stack.

Ok Narue I will certailny try that after going home and see if it runs or not and let you know what happens tomorrow.
Could you tell me more about "aliasing" please, if possible...:)

char* ar2 = new char(strlen(ar));
array[top] = new char (strlen(ele));

oh my god, you are allocating one character, and then you are setting that character to the character that has the ASCII value that is the length of some other string

look at it

Thanks Narue again.
I now realized what I was doing. ;)
I was just allocating the memory to store the string but not passing it "actually" to th stack.
Thanks again, appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.