How to fix 'std::bad_alloc'

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.

#include <iostream>
#include <fstream>
#include <string>
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
using namespace std;
template <typename T> 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>=cima; i--) cout<< Tab[i]<< " "; }
};
template <typename V> class Queue
{
public: int lunghezza; Queue(int l){ lunghezza=l; }; Pila<V> A= Pila <V>(lunghezza); Pila<V> B= Pila <V>(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<< " Coda Vuota"<< endl; } else A.Pop(); } void Stampa(){ A.Print(); cout<< 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<100){ infile>> tipo; infile>> N; if(tipo=="int"){ Queue<int> A(N); for(int i=0; i<N; i++){ infile>> operazione; if(operazione=="dequeue") A.dequeue(); else { int elem=stoi(operazione.substr(1)); A.enqueue(elem); } } A.Stampa(); } if(tipo=="double"){ Queue<double> A(N); for(int i=0; i<N; i++){ infile>> operazione; if(operazione=="dequeue") A.dequeue(); else { double elem=stod(operazione.substr(1)); A.enqueue(elem); } } A.Stampa(); } if(tipo=="bool"){ Queue<bool> A(N); for(int i=0; i<N; i++){ infile>> operazione; if(operazione=="dequeue") A.dequeue(); else { bool elem=stoi(operazione.substr(1)); A.enqueue(elem); } } A.Stampa(); } if(tipo=="char"){ Queue<char> A(N); for(int i=0; i<N; i++){ infile>> operazione; if(operazione=="dequeue") A.dequeue(); else { char elem=(char)(operazione[1]); A.enqueue(elem); } } A.Stampa(); } c++; }
}
18

1 Answer

Your first problem is in your initialization.

int lunghezza;
Queue(int l){ lunghezza=l;
};
Pila<V> A= Pila <V>(lunghezza);
Pila<V> B= Pila <V>(lunghezza);

A and B will be initialized with the value of lunghezza which is not yet initialized. This way you can get a very high value that allocate lots of memory.

A better implementation of this part is to use initializers

int lunghezza;
Queue(int l) : lunghezza(l), A(l), B(l)
{
}
Pila<V> A;
Pila<V> B;

You second problem is in Pila class.

Pila(int L){ lunghezza=L; Tab= new T[L]; cima=lunghezza;
}

In this code, Tab is never disallocated so you will leak (loose) the allocated memory when your object will be destroyed. You must call delete[] Tab; in your destructor.

You can also use STL classes such as std::list or std::vector to replace your class Pila.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like