The following code worked fine for me (code blocks 10.05) and showed no compile-time/runtime errors for various test cases. But showed runtime error as I submitted it online on a programming website.
#include<stdio.h>
#include<stdlib.h>
/*
Here comes newPos()
*/
int main()
{
int t,i,n,k,j;
scanf("%d",&t);
int* a;
for(i=0;i<t;i++)
{
scanf("%d",&n);
free(a);
a=(int*) malloc(n);
for(j=0;j<n;j++)
scanf("%d",&a[j]);
scanf("%d",&k);
printf("%d\n",newPos(a,n,k));
}
return 0;
}
And then I changed it into a .cpp file after making a few changes. i.e., instead of free(a) I used the statement, delete a; and instead of a=(int*) malloc(n), I used the statement, a=new int[n]; Then it executed successfully both on my compiler and online.