0

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.

Prasanjit Rath
  • 166
  • 2
  • 13
  • 5
    On top of the duplicate, using `free` on pointer allocated with `new` is Undefined Behaviour. You have to use `delete` for `new`, `delete[]` for `new[]` and `free` for `malloc` or `calloc`, else results can be disastrous. – Yksisarvinen Oct 12 '19 at 14:17
  • 5
    It's not accessible. Attempting to access it is also undefined behavior and can do anything, including *not* crashing. – HolyBlackCat Oct 12 '19 at 14:17
  • 4
    You are incorrect in your assumption that memory starts out holding NULL, it may actually be anything. – Mark Ransom Oct 12 '19 at 14:17
  • Cleaning up memory you allocate is not *strictly necessary*, since once the program terminates, the OS will clean up any allocated memory. But, not cleaning up as you go may leave your program with an abnormally large memory use *while it runs*. – Jesper Juhl Oct 12 '19 at 14:21
  • 2
    ***I thought that memory is a bank containing nothing(NULL), when you allocate memory dynamically*** This is likely a function of an observation and is related to what your OS does to protect information leakage from other processes however it is strictly not a function of `c++` and this behavior can not be relied upon. Basically your OS will give your application pages that are zeroed because it does not want your application to see the previous memory contents from some other application. However after your application gets these pages and you new / delete the pages will not be zeroed. – drescherjm Oct 12 '19 at 14:23
  • 1
    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" - Well, you were *wrong*. Accessing freed memory is [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub). You are *not* allowed to do that. And the consequences of invoking UB in one part of your program may have disastrous results in *other* parts of your program. Don't invoke UB! This is relevant: https://stackoverflow.com/a/6445794/5910058 – Jesper Juhl Oct 12 '19 at 14:36

0 Answers0