If I allocate some memory dynamically, say an array and in the end decide to free it. Then, all that happens is that the value pointed by the array pointer is set to 0 and I can still use the pointers as before. So, what was the point of writing an extra 'free' function, as I had an option to assign 0 to values of arr?
int* arr=new int[2];
cout<<arr[0]<<" "<<arr[1]<<endl; //Prints 0 0
arr[0]=5;arr[1]=6;
cout<<arr[0]<<" "<<arr[1]<<endl; //Prints 5 6
free(arr);
cout<<arr[0]<<" "<<arr[1]<<endl; //Prints 0 0
//I could have done above step with arr[0]=0 and arr[1]=0
arr[0]=5;arr[1]=6;
cout<<arr[0]<<" "<<arr[1]<<endl; //Prints 5 6
I expected that freeing operation should somehow mean that "Okay, since you are done with your program, now this pointer and its values hold no significance and hence are getting destroyed and won't be accessible by you anymore"
I thought that memory is a bank containing nothing(NULL), when you allocate memory dynamically, the NULL is actually changed to something significant data(so we say memory is now being consumed) and when freed again goes back to NULL.
But actual output says even after freeing a pointer, I can still use the memory.