I read somewhere that it is disastrous to use free
to get rid of an object not created by calling malloc
, is this true? why?
相关问题
- Multiple sockets for clients to connect to
- What uses more memory in c++? An 2 ints or 2 funct
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Achieving the equivalent of a variable-length (loc
It would certainly be possible for an implementation of
malloc
/free
to keep a list of the memory blocks thats been allocated and in the case the user tries to free a block that isn't in this list do nothing.However since the standard says that this isn't a requirement most implementation will treat all pointers coming into free as valid.
It is undefined behaviour. And logically, if behaviour is undefined, you cannot be sure what has happened, and if the program is still operating properly.
Strictly speaking, this is not true. calloc() and realloc() are valid object sources for free(), too. ;)
That's undefined behavior - never try it.
Let's see what happens when you try to
free()
an automatic variable. The heap manager will have to deduce how to take ownership of the memory block. To do so it will either have to use some separate structure that lists all allocated blocks and that is very slow an rarely used or hope that the necessary data is located near the beginning of the block.The latter is used quite often and here's how i is supposed to work. When you call malloc() the heap manager allocates a slightly bigger block, stores service data at the beginning and returns an offset pointer. Smth like:
then
free()
will try to access that data by offsetting the passed pointer but if the pointer is to an automatic variable whatever data will be located where it expects to find service data. Hence undefined behavior. Many times service data is modified byfree()
for heap manager to take ownership of the block - so if the pointer passed is to an automatic variable some unrelated memory will be modified and read from.Implementations may vary but you should never make any specific assumptions. Only call
free()
on addresses returned bymalloc()
family functions.Please have a look at what undefined behavior means.
malloc()
andfree()
on a conforming hosted C implementation are built to standards. The standards say the behavior of callingfree()
on a heap block that was not returned bymalloc()
(or something wrapping it, e.g.calloc()
) is undefined.This means, it can do whatever you want it to do, provided that you make the necessary modifications to
free()
on your own. You won't break the standard by making the behavior offree()
on blocks not allocated bymalloc()
consistent and even possibly useful.In fact, there could be platforms that (themselves) define this behavior. I don't know of any, but there could be some. There are several garbage collecting / logging malloc() implementations that might let it fail more gracefully while logging the event. But thats implementation , not standards defined behavior.
Undefined simply means don't count on any kind of consistent behavior unless you implement it yourself without breaking any defined behavior. Finally, implementation defined does not always mean defined by the host system. Many programs link against (and ship) uclibc. In that case, the implementation is self contained, consistent and portable.
By the standard, it's "undefined behavior" - i.e. "anything can happen". That's usually bad things, though.
In practice:
free
'ing a pointer means modifying the heap. C runtime does virtually never validate if the pointer passed comes from the heap - that would be to costly in either time or memory. Combine these two factoids, and you get "free(non-malloced-ptr) will write something somewhere" - the resutl may be some of "your" data modified behind your back, an access violation, or trashing vital runtime structures, such as a return address on the stack.Example: A disastrous scenario:
Your heap is implemented as a simple list of free blocks. malloc means removing a suitable block from the list, free means adding it to the list again. (a typical if trivial implementation)
You free() a pointer to a local variable on the stack. You are "lucky" because the modification goes into irrelevant stack space. However, part of the stack is now on your free list.
Because of the allocator design and your allocation patterns, malloc is unlikely to return this block. Later, in an completely unrelated part of the program, you actually do get this block as malloc result, writing to it trashes some local variables up the stack, and when returning some vital pointer contains garbage and your app crashes. Symptoms, repro and location are completely unrelated to the actual cause.
Debug that.