I have to do some kind of project and I'm stuck. I'm getting an bad_alloc error. I checked code a lot of times, tried to google some solutions, but still nothing, that's why I'm writing here.The thing is the program runs normally, but at the task manager he memory usage raises to 2GB(that's the limit as I know) and then it crashes. The program needs to check time of allocating space and copying variables. Here's part of the code:
class Table
{
int *tablica;
int size;
public:
Table()
{
tablica = NULL;
size = 0;
}
~Table()
{
delete tablica;
size = 0;
}
int *push_back(int val)
{
int *temp = new int[size];
if(size % 10 == 0)
{
for(int i = 0; i < size; i++)
temp[i] = tablica[i];
tablica = new int[size + 10];
for(int i = 0; i < size; i++)
tablica[i] = temp[i];
}
tablica[size] = val;
size++;
delete []temp;
return tablica;
}
void test()
{
LONGLONG measure[100][6];
LARGE_INTEGER performanceCountStart, performanceCountEnd;
int cpy_tab [20000];
for(int j = 0; j < 100; j++)
{
for(int i = 0; i < 20000; i++)
cpy_tab[i] = rand() % 10000 - 10000;
performanceCountStart = startTimer();
for(int i = 0; i < 500; i++)
{
push_back(cpy_tab[i]);
}
performanceCountEnd = endTimer();
measure[j][0] = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
cout<<j<<"."<<measure[j][0]<<endl;
delete []tablica;
size = 0;
performanceCountStart = startTimer();
for(int i = 0; i < 2000; i++)
{
push_back(cpy_tab[i]);
}
performanceCountEnd = endTimer();
measure[j][1] = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
cout<<j<<"."<<measure[j][1]<<endl;
delete []tablica;
size = 0;
performanceCountStart = startTimer();
for(int i = 0; i < 4000; i++)
{
push_back(cpy_tab[i]);
}
performanceCountEnd = endTimer();
measure[j][2] = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
cout<<j<<"."<<measure[j][2]<<endl;
delete []tablica;
size = 0;
performanceCountStart = startTimer();
for(int i = 0; i < 8000; i++)
{
push_back(cpy_tab[i]);
}
performanceCountEnd = endTimer();
measure[j][3] = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
cout<<j<<"."<<measure[j][3]<<endl;
delete []tablica;
size = 0;
performanceCountStart = startTimer();
for(int i = 0; i < 14000; i++)
{
push_back(cpy_tab[i]);
}
performanceCountEnd = endTimer();
measure[j][4] = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
cout<<j<<"."<<measure[j][4]<<endl;
delete []tablica;
size = 0;
performanceCountStart = startTimer();
for(int i = 0; i < 20000; i++)
{
push_back(cpy_tab[i]);
}
performanceCountEnd = endTimer();
measure[j][5] = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
cout<<j<<"."<<measure[j][5]<<endl;
delete []tablica;
size = 0;
}
}
Any ideas of resolving this problem would be really worthy to me!