简体   繁体   中英

Why are the contents pointed to by a pointer not changed when memory is deallocated using free()?

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?

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 .

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!

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.

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.

Actually the C programming language specifies that after the lifetime of the object, even the value of any pointer pointing to it becomes indeterminate , ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM