H
Hassan Iqbal
hi,
in the code below i find that i am able to access p even after i have
freed it.
not only that the previous values stored in p are accessible even
after reallocation of memory to p. please some one explain it to me.
what does malloc and free do here?
thanks,
hassan
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i,n,*p;
printf("\n give value for n: ");
scanf("%d", &n);
p= (int *)malloc(n*sizeof(int ));
printf("\n give %d integers: ", n);
for(i=0;i<n;i++){
printf("\nReadig int...");
scanf("%d", (p+i));
printf("\nThe value is %d ", *(p+i));
}
printf("\n the given integers are: ");
free(p);
p= (int *)malloc(n*sizeof(int ));
for(i=0;i<n;i++)
printf("%d ", *(p+i));
//free(p);
printf("\n\n");
return 0;
}
in the code below i find that i am able to access p even after i have
freed it.
not only that the previous values stored in p are accessible even
after reallocation of memory to p. please some one explain it to me.
what does malloc and free do here?
thanks,
hassan
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i,n,*p;
printf("\n give value for n: ");
scanf("%d", &n);
p= (int *)malloc(n*sizeof(int ));
printf("\n give %d integers: ", n);
for(i=0;i<n;i++){
printf("\nReadig int...");
scanf("%d", (p+i));
printf("\nThe value is %d ", *(p+i));
}
printf("\n the given integers are: ");
free(p);
p= (int *)malloc(n*sizeof(int ));
for(i=0;i<n;i++)
printf("%d ", *(p+i));
//free(p);
printf("\n\n");
return 0;
}