-2

I am Confused and thinking about Free Pointer in Following way , can someone clarify

I always thought that the free function just removes the address stored in the pointer, hence breaking the connection between the pointer and the space allocated for block. At the end making the values at the space a garbage value and hence making it virtually free to use by another program.

Please specify the reason why I am wrong.

Also, What happens when you point two pointers to a single space allocated and Try to free them both? Does it Gives a compilation Error / Run Time Error?

Kara
  • 6,115
  • 16
  • 50
  • 57
  • You should ask one question at a time, and search before posting to find whether it has already been answered. Did you try any basic searches like _C what does free do_ or _C double free_? This will be a duplicate of [C - What does free() do to the memory?](https://stackoverflow.com/questions/28454123/c-what-does-free-do-to-the-memory) and/or [What does “double free” mean?](https://stackoverflow.com/questions/21057393/what-does-double-free-mean) or any of tens of other equivalent questions. – underscore_d Oct 25 '17 at 14:18
  • also: [Why does free not clear out a pointers value?](https://stackoverflow.com/questions/33840319/why-does-free-not-clear-out-a-pointers-value) – underscore_d Oct 25 '17 at 14:31

1 Answers1

1

free(ptr) de-allocates the memory block pointed to by its argument ptr, that is it returns the block to the free pool. Consecutive calls to malloc() may use that memory, re-allocating it for another purpose.

However, free() receives its argument as a copy, that is it gets the value from the ptr variable, not the variable itself. As a result it can't 'clear the pointer'. If you expect the ptr variable becomes NULL, you're wrong. That's a common mistake. You should assign ptr = NULL yourself after free() to make sure the variable value becomes unusable.

As for the memory block itself: free() is not obliged to 'make it garbage' or clear it. You can try accessing the freed memory by the same pointer, and often you will see the previous content still present there.
But don't rely on it! The memory can be reallocated for another purpose almost any time (the freed memory might even become unavailable for the process), so fiddling with it may invoke unpredictable results (so-called Undefined Behavior).

An attempt to deallocate a block twice (i.e. a second free() with the same pointer) is UB, too.

CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • Thank You for your fast Response. Kindly Explain in a Little Simpler Manner . What is UB ? – Kunal Chand Oct 25 '17 at 18:53
  • @KunalChand UB = Undefined Behaviour = anything can happen, from the progam continuing as if everything was correct and even returning correct results, through any unreasonable or even weird results up to sudden crash of the proces and program termination. – CiaPan Oct 25 '17 at 18:58
  • UB is behaviour that the Standard does not define, which therefore renders the result of an offending program undefined, meaning that it may fail to compile, work 100% perfectly until it horribly breaks 3 years later, or even make you a nice cup of tea. In other words, avoid it as an absolute rule. You may ask why such a category of behaviour exists? Well, (A) again, just search about it, and (B) because C is designed to be a fast language, not a safe one, so the onus is on the programmer not to invoke UB, instead of being on the language to waste cycles saving the programmer from themselves. – underscore_d Oct 25 '17 at 19:23