Hi,
After deallocating dynamically allocated memory, I was still accessible to access the data present in that memory. Can u please help me out why this is happening. This happened with c++(new and delete) operators as well. What is the logic behind. Here is the program I tested in C.
#include<stdio. h>
#include<stdlib .h>
#include<string .h>
int main()
{
char *ptr;
char *str="kranthi";
int len;
len=strlen(str) ;
ptr=(char *)malloc(len+1) ;
if(ptr==NULL)
printf("memory allocation failed");
else
{
strcpy(ptr, str);
printf("data is %s \n",ptr);
}
free(ptr);
printf("\n after freeing the memory ");
printf("data is %s \n",ptr);
return 0;
}
After deallocating dynamically allocated memory, I was still accessible to access the data present in that memory. Can u please help me out why this is happening. This happened with c++(new and delete) operators as well. What is the logic behind. Here is the program I tested in C.
#include<stdio. h>
#include<stdlib .h>
#include<string .h>
int main()
{
char *ptr;
char *str="kranthi";
int len;
len=strlen(str) ;
ptr=(char *)malloc(len+1) ;
if(ptr==NULL)
printf("memory allocation failed");
else
{
strcpy(ptr, str);
printf("data is %s \n",ptr);
}
free(ptr);
printf("\n after freeing the memory ");
printf("data is %s \n",ptr);
return 0;
}
Comment