I'm getting the double free or corruption (!prev) error while implementing the following function on an online judge.
void nextPermutation(int* A, int n1)
{
int i = 0;
int tmp = 0;
int flag = 0;
int ret = 0;
if(n1 == 1)
return A[0];
for(i = n1; i > 0; i--)
{
if(flag == 0)
{
if(A[i] > A[i - 1])
{
tmp = A[i];
A[i] = A[i - 1];
A[i - 1] = tmp;
flag = 1;
ret = i;
break;
}
}
}
for(i = ret; i < n1 - 1; i++)
{
if(A[i] > A[i + 1])
tmp = A[i];
A[i] = A[i + 1];
A[i + 1] = tmp;
}
}
However, when I test the code using custom inputs, it works fine.
Could anyone tell me why does this happen?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…