Every 1 second, function works.
my system the linux.
Runs suddenly dies.
-----global-------
static int arrayNum[33000];
-------------------
function(){
unsigned short int** US_INT;
US_INT= new unsigned short int*[255];
for(int i = 0; i < 255; i++)
{
US_INT[i] = new unsigned short int[128];
memset(US_INT[i], 0, sizeof(unsigned short int) * 128);
}
double x;
double y;
int cnt= 0;
int nArrayCount=0;
for(int i = 0; i < 255; i++)
{
for(int j=0;j<128;j++){
x=j;
y=cnt
nArray[nArrayCount]=US_INT[i][j];
nArrayCount++;
}
cnt=cnt+(256/255);
}
for(int i = 0; i < 255; i++)
{
delete US_INT[i];
}
delete[] US_INT;
}
program stop. and message↓ terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
The bad_alloc
exception is triggered by a failure in the memory allocation (so one of your new
). terminate()
is called automatically because you don't catch this exception.
The root cause of the bad_alloc is that you don't have enough memory (or the free store is corrupted). This could for example happen if you reapeatedly fail to free memory in some loops.
In fact, in your code, it appears that you don't delete correctly the arrays US_INT[i]
. Your must use delete[]US_INT[i]
. As a general rule, every time you use new[]
, you shall use delete[]
.
P.S.: You could also opt for vectors instead of arrays and free your mind from memory maangement issues.