I am a newbie when it comes to dynamic memory allocation. When we free the memory using void free(void *ptr)
the memory is deallocated but the contents of the pointer are not deleted. Why is that? Is there any difference in more recent C compilers?

- 32,567
- 20
- 113
- 146

- 814
- 1
- 15
- 40
-
2Why do you see this as a problem ? – Paul R Jun 28 '12 at 07:16
-
1@PaulR, when one is used to managed languages, everything about pointers would be seen as a problem. – ugoren Jun 28 '12 at 07:17
-
3Are you asking about value of `ptr` not changing, or the memory it points to? – unkulunkulu Jun 28 '12 at 07:19
5 Answers
Computers don't "delete" memory as such, they just stop using all references to that memory cell and forget that anything of value is stored there. For example:
int* func (void)
{
int x = 5;
return &x;
}
printf("%d", *func()); // undefined behavior
Once the function has finished, the program stops reserving the memory location where x
is stored, any other part of the program (or perhaps another program) is free to use it. So the above code could print 5, or it could print garbage, or it could even crash the program: referencing the contents of a memory cell that has ceased to be valid is undefined behavior.
Dynamic memory is no exception to this and works in the same manner. Once you have called free()
, the contents of that part of the memory can be used by anyone.
Also, see this question.

- 195,001
- 40
- 254
- 396
-
Very nice, succinct illustration of the issue with valid code example. A++ would buy again. – tbert Jun 28 '12 at 07:42
The thing is that accessing memory after it has been freed is undefined behavior. It's not only that the memory contents are undefined, accessing them could lead to anything. At least some compilers when you build a debug version of the code, actually do change the contents of the memory to aid in debugging, but in release versions it's generally unnecessary to do that, so the memory is just left as is, but anyway, that is not something you can safely rely upon, don't access freed memory, it's unsafe!

- 11,576
- 2
- 31
- 49
In C, parameters are passed by value. So free
just can't change the value of ptr
.
Any change it would make would only change the value within the free
function, and won't affect the caller's variable.
Also, changing it won't be so much help. There can be multiple pointers pointing to the same piece of memory, and they should all be reset when freeing. The language can't keep track of them all, so it leaves the programmer to handle the pointers.

- 16,023
- 3
- 35
- 65
-
2I disagree with the your first assertion. The OP is interested in why `the contents to which \`ptr\` points` are not cleared/deleted. This is very much feasible but unnecessary. ie `free` can clear the area to which `ptr` points to without changing the caller's variable as you seem to suggest. Your answer is focusing on the pointer and not the area to which it points which the OP is interested in – Pavan Manjunath Jun 28 '12 at 07:42
-
I see that the question can be interpreted in two ways (but still think that my understanding is what he meant). unkulunkulu asked it directly in a comment, and wasn't answered yet. – ugoren Jun 28 '12 at 09:54
-
The question is about the contents of the pointer (mentioned twice). I would definitely go with ugoren's interpretation. In any case, there was no wrong assertion, even if there was a misinterpretation, and it's a good answer given the interpretation, so ... +1 – Jim Balter Jun 28 '12 at 10:50
This is very normal, because clearing the memory location after free is an overhead and generally not necessary. If you have security concerns, you can wrap the free call within a function which clears the region before freeing. You'll also notice that this requires the knowledge of the allocation size, which is another overhead.

- 94,503
- 21
- 155
- 181
Actually the C programming language specifies that after the lifetime of the object, even the value of any pointer pointing to it becomes indeterminate, i.e. you can't even depend on the pointer to even retain the original value.
That is because a good compiler will try to aggressively store all the variables into the CPU registers instead of memory. So after it sees that the program flow calls a function named free
with the argument ptr
, it can mark the register of the ptr
free for other use, until it has been assigned to again, for example ptr = malloc(42);
.
In between these two it could be seen changing the value, or comparing inequal against its original value, or other similar behaviour. Here's an example of what might happen.

- 129,958
- 22
- 279
- 321