I've been running some tests trying to understand how the free()
function works. Here is my code followed by some questions based on what I have understood.
int n=0,i=0;
scanf("%d",&n);
int *A = (int*)malloc(n*sizeof(int));
for (i=0;i<n;i++)
{
A[i]=2*i;
printf("A[%d] = %d\n",i,*(A+i));
}
printf("Address of A is = %d\n",A);
free(A);
for (i=0;i<n;i++)
{
printf("%d\n",*(A+i));
}
printf("Address of A is = %d\n",A);
What I don't understand is why after free(A)
, the values of A are still the same? Shouldn't there be some garbage value after free? And why did the address of A remain the same? What really happened after freeing A in terms of memory?