title: How to fix 'std::bad_alloc'
body: <p>I have to read from file 100 task of a different kind in order to do different operation on a Queue, and print on an output file.
  But I get "terminate called after throwing an instance of 'std::bad_alloc' "
  As Error
  I think is something in template declaration.
  I tried to declare it in different ways but seems not to work.
  If someone can help me I'll be very gratefull.</p>
  
  <pre><code>#include    &lt;iostream&gt;
  #include    &lt;fstream&gt;
  #include    &lt;string&gt;
  
  #define INPUT_FILE "input.txt"
  #define OUTPUT_FILE "output.txt"
  using namespace std;
  
  
  template &lt;typename T&gt; class Pila
  {
  private:
  bool empty=1;
  public:
  int lunghezza;
  int cima;
  T * Tab;
      Pila(int L){
          lunghezza=L;
          Tab= new T[L];
          cima=lunghezza;
      }
      void Push(T L){  
          cima--;
          Tab[cima]=L;
      }
  
      void Pop(){
  
          if(!empty)
          cima++;
          if(cima=lunghezza) empty=1;
      }
  
      bool Empty(){
         if(empty==1) return 1;
         else return 0;
      }
  
      T top(){
          return Tab[cima];
      }
  
  
      void Print(){
          for(int i=lunghezza-1; i&gt;=cima; i--) cout&lt;&lt; Tab[i]&lt;&lt; " ";
      }
  };
  
  template &lt;typename V&gt; class Queue
  {
  
  public:
      int lunghezza;
  
      Queue(int l){
          lunghezza=l;
      };
      Pila&lt;V&gt; A= Pila &lt;V&gt;(lunghezza);
      Pila&lt;V&gt; B= Pila &lt;V&gt;(lunghezza);
  
      void enqueue(V L){ //sposta tutti gli elementi da A a B 
          while(!A.Empty()){ 
              B.Push(A.top());
              A.Pop();
          }
  
          A.Push(L); //Mette L dentro A
  
          while(!B.Empty()){   //sposta tutto di nuovo dentro A
              A.Push(B.top());
              B.Pop();
          }
      }
  
  
      void dequeue(){
          if(A.Empty()){
              cout&lt;&lt; " Coda Vuota"&lt;&lt; endl;
          }
          else       
          A.Pop();
      }    
  
      void Stampa(){
              A.Print();
              cout&lt;&lt; endl; 
      }
  };
  
  int main(){
  
  
      fstream infile, outfile;
  
      infile.open(INPUT_FILE, fstream::in);
      outfile.open(OUTPUT_FILE, fstream::out);
  
  
      int c=0, N,tmp;
      string tipo,operazione;
      while(c&lt;100){
          infile&gt;&gt; tipo;
          infile&gt;&gt; N;
         if(tipo=="int"){
              Queue&lt;int&gt; A(N);
              for(int i=0; i&lt;N; i++){
                  infile&gt;&gt; operazione;
                  if(operazione=="dequeue")
                  A.dequeue();
                  else
                  {
                      int elem=stoi(operazione.substr(1));
                      A.enqueue(elem);
                  }
  
              }
              A.Stampa();
          }
  
          if(tipo=="double"){
              Queue&lt;double&gt; A(N);
              for(int i=0; i&lt;N; i++){
                  infile&gt;&gt; operazione;
                  if(operazione=="dequeue")
                  A.dequeue();
                  else
                  {
                      double elem=stod(operazione.substr(1));
                      A.enqueue(elem);
                  }
  
              }
              A.Stampa();
          }
  
  
          if(tipo=="bool"){
              Queue&lt;bool&gt; A(N);
              for(int i=0; i&lt;N; i++){
                  infile&gt;&gt; operazione;
                  if(operazione=="dequeue")
                  A.dequeue();
                  else
                  {
                      bool elem=stoi(operazione.substr(1));
                      A.enqueue(elem);
                  }
  
              }
              A.Stampa();          
          }
  
          if(tipo=="char"){
              Queue&lt;char&gt; A(N);
              for(int i=0; i&lt;N; i++){
                  infile&gt;&gt; operazione;
                  if(operazione=="dequeue")
                  A.dequeue();
                  else
                  {
                      char elem=(char)(operazione[1]);
                      A.enqueue(elem);
                  }
  
              }
             A.Stampa();           
          }
  
  
          c++;
      }
  
  }
  </code></pre>