why this code is showing double free corruption?Code:int main()
{
int *a,i;
a=(int *)malloc(10*sizeof(int));
for(i=0;i<10;i++)
*(a+i)=i*i;
for(i=0;i<10;i++)
printf("%d",*a++);
free(a);
return 0;
}
Printable View
why this code is showing double free corruption?Code:int main()
{
int *a,i;
a=(int *)malloc(10*sizeof(int));
for(i=0;i<10;i++)
*(a+i)=i*i;
for(i=0;i<10;i++)
printf("%d",*a++);
free(a);
return 0;
}
It may not be a double free per se, but you are incrementing a. Rather, have another pointer to be a copy of a, and increment that. That way, free(a) will operate on the original location pointed to by a.
This smells like a test question.
What is a? What is its value prior to the final loop? What about after? How can that affect the free operation?
Thanks laser
I have used a+i in place of a++ it fine now.
But another doubt
How this code is giving 16?Code:main()
{
double d;
printf("%d", ((double *)0+2));
}
Because %d does NOT print doubles.
r2r is right, you're just trotting off quiz questions.
He's printing a pointer value there. 16 would be the expected offset from NULL.
%d does not print pointers either. That's undefined behavior.
Furthermore, main must have a return type of main and a return 0; at the end.