I have following C++ code for bubble sort. This code compiles without any error, but when I re compile and run, I get
*** stack smashing detected ***: terminated
As a C++ newby I want to know ,why do I get these occasional errors when it runs?
void bubbleSort(int eatenPanCakes[10],int arrSize){
int temp=0;
for(int i=0;i<arrSize-1;i++){
for (int j = 0; j < arrSize-i; j++)
{
if (eatenPanCakes[j] > eatenPanCakes[j+1])
{
temp = eatenPanCakes[j+1];
eatenPanCakes[j+1] = eatenPanCakes[j];
eatenPanCakes[j] = temp;
}
}
}
}
Environment : g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
There is a bug in my code : for (int j = 0; j+1 < arrSize-i; j++)
would be the right algorithm and that works without error.