#include<iostream>
using namespace std;
int main()
{
int *ptr;
int n=5,i;
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL){
cout<<"Memory not allocated"<<endl;
return 0;
}
else{
cout<<"Memory allocated"<<endl;
for(i=0;i<n;i++){
ptr[i]=i+1;
}
for(i=0;i<n;i++){
cout<<ptr[i]<<" ";
}
free(ptr);
cout<<"\nMemory Freed\n";
for(i=0;i<n;i++){
cout<<ptr[i]<<" ";
}
}
return 0;
}
**OUTPUT:
Memory allocated
1 2 3 4 5
Memory Freed
18682760 18677952 3 4 5**
Why is the OUTPUT after free(ptr) is some numbers. I'm new to dynamic programming so I experimented with malloc() and calloc() when i observed this. I thought after using free(ptr) i thought the array will be NULL but it still contains some values and when i run the program again the value after free(ptr) changes. Can anyone explain ?