问题
I have been taught in lectures, that calling free()
on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL
, right after having freed it.
However, I still have never heard any explanation as to why that is. From what I understand, the way malloc()
works, it should technically keep track of the pointers it has allocated and given you to use. So why does it not know, whether a pointer it receives through free()
has been freed yet or not?
I would love to understand, what happens internally, when you call free()
on a location that has previously already been freed.
回答1:
When you use malloc
you are telling the PC that you want to reserve some memory location on the heap just for you. The computer gives back a pointer to the first byte of the addressed space.
When you use free
you are actually telling the computer that you don't need that space anymore, so it marks that space as available for other data.
The pointer still points to that memory address. At this point that same space in the heap can be returned by another malloc
call. When you invoke free
a second time, you are not freeing the previous data, but the new data, and this may not be good for your program ;)
回答2:
To answer your first question,
So why does it not know, whether a pointer it receives through
free()
has been freed yet or not?
because, the specification for malloc()
in C standard does not mandate this. When you call malloc()
or family of functions, what it does is to return you a pointer and internally it stores the size of the memory location allocated in that pointer. That is the reason free()
does not need a size to clean up the memory.
Also, once free()
-d, what happens with the actually allocated memory is still implelentation dependent. Calling free()
is just a marker to point out that the allocated memory is no longer in use by the process and can be reclaimed and e re-allocated, if needed. So, keeping track of the allocated pointer is very needless at that point. It will be an unnecessary burden on the OS to keep all the backtracks.
For debugging purpose, however, some library implementations can do this job for you, like DUMA or dmalloc and last but not the least, memcheck tool from Valgrind.
Now, technically, the C
standard does not specify any behaviour if you call free()
on an already free-ed pointer. It is undefined behavior.
C11
, chapter §7.22.3.3, free()
function
[...] if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to
free()
orrealloc()
, the behavior is undefined.
回答3:
When you are calling malloc
you are getting a pointer. The runtime library needs to keep track of the malloc
ed memory. Typically malloc
does not store the memory management structures separated from the malloc
ed memory but in one place. So a malloc
for x bytes in fact takes x+n bytes, where one possible layout is that the first n bytes are containing a linked list struct with pointers to the next (and maybe previous) allocated memory block.
When you free
a pointer then the function free
could walk through it's internal memory management structures and check if the pointer you pass in is a valid pointer that was malloc
ed. Only then it could access the hidden parts of the memory block. But doing this check would be very time consuming, especially if you allocate a lot. So free
simply assumes that you pass in a valid pointer. That means it directly access the hidden parts of the memory block and assumes that the linked list pointers there are valid.
If you free
a block twice then you might have the problem that someone did a new malloc
, got the memory you just freed, overwrites it and the second free
reads invalid pointers from it.
Setting a free
d pointer to NULL
is good practice because it helps debugging. If you access free
d memory your program might crash, but it might also just read suspicious values and maybe crash later. Finding the root cause then might be hard. If you set free
d pointers to NULL
your program will immediately crash when you try to access the memory. That helps massively during debugging.
回答4:
C standard only says that calling free
twice on a pointer returned by malloc
and its family function invoke undefined behavior. There is no further explanation why it is so.
But, why it is bad is explained here:
Freeing The Same Chunk Twice
To understand what this kind of error might cause, we should remember how the memory manager normally works. Often, it stores the size of the allocated chunk right before the chunk itself in memory. If we freed the memory, this memory chunk might have been allocated again by another
malloc()
request, and thus this double-free will actually free the wrong memory chunk - causing us to have a dangling pointer somewhere else in our application. Such bugs tend to show themselves much later than the place in the code where they occured. Sometimes we don't see them at all, but they still lurk around, waiting for an opportunity to rear their ugly heads.Another problem that might occure, is that this double-free will be done after the freed chunk was merged together with neighbouring free chunks to form a larger free chunk, and then the larger chunk was re-allocated. In such a case, when we try to
free()
our chunk for the 2nd time, we'll actually free only part of the memory chunk that the application is currently using. This will cause even more unexpected problems.
来源:https://stackoverflow.com/questions/34284846/calling-free-on-a-pointer-twice