tags:

views:

127

answers:

5

Possible Duplicate:
C programming : How does free know how much to free?

When we are allocating some memory from the heap using mallolc or calloc or even realloc, we are giving the pointer and the size of the memory which we want to allocate. But while freeing that allocated memory, we are providing only pointer (base address of the allocated memory). So my Question is how the free() function knows how much memory it has to free. means the size of the memory.

A: 

One correction here - malloc() wants only the size, and returns the pointer.

As for free(), c-runtime library maintains the list of all blocks that malloc() returned, and when you free(), it searches that list for your pointer, then releases the memory and updates the list.

This list is called a heap.

Daniel Mošmondor
No it doesn't do that.
EJP
I guess that depends on the implementation of the crt library, and I provided the generalized description of the heap.
Daniel Mošmondor
I've never seen an implementation that does that in nearly 30 years, and I've written a few myself.. It would be inefficient and pointless. The size can be recovered from the block being freed in O(1). Why would you search a list?
EJP
To make it easier to merge the newly freed block with potentially free blocks before and after. Given you probably need to search the free list to find those, might as well keep the size there for locality ...
Paul
You are confusing the free list with the non-existent list of allocated blocks that he claimed was searched.
EJP
+1  A: 

It's stored in the block returned by malloc/calloc.

EJP
@EJP: This is not always the case. How a given runtime is implemented is not part of the C specification. The implementor is free to do it any way they want, so long as the specified interface is followed. Each C compiler generally comes with its own C-Runtime Library and each of these varies in how they implement the memory management functions.
Tergiver
So please show me a case where it isn't the case.
EJP
@EJP: when you say that it's stored in the block, does it mean that the system allocates extra memory at the end of the allocated memory and stores the size there? Can you please elaborate as this thread is getting very interesting indeed.
Nir Levy
Yes, either at the end or before the beginning.
EJP
I worked on a PIC controller many years ago. That chip provided its own form of rudamentary memory management (memory was built into the chip). The runtime simply used this internal mechanism.
Tergiver
And how did the chip know how big the returned block was? Did it search a list? or embed the size in the block?
EJP
+1  A: 

You cannot free part of the memory which was allocated in a single call.

Each call to alloc() sets aside a block of memory. Memory that was alloc'ed as a block in one call must also be freed as a block in one call. Since there is no way to free part of this memory, there is no need to specify the amount of memory to be freed in the call to free().

The system keeps track of how much memory was allocated for each pointer. When you free the pointer, the system already knows exactly how much memory was allocated, and thus, how much to free.

Erick Robertson
"You cannot free part of the memory which was allocated in a single call." Yes, you can. See realloc (e.g. http://www.itee.uq.edu.au/~comp2303/Leslie_C_ref/C/MAN/realloc.htm). Realloc may return a different pointer, or it may just adjust the allocated and return the same pointer.
Paul
The question was about `free`, and you cannot do this using `free`.
Erick Robertson
+2  A: 

There are several techniques. One is to store information at addresses before the base address. Another is to maintain a record of allocated addresses and sizes separately.

See the memory management reference for lots of information on memory management and the discussion of free lists etc.

Paul
A: 

It depends on the specific malloc/free implementation that you are using. Typically there might be some kind of linked list of allocated blocks and another linked list of free blocks. Each block will typically have a header which contains the size of the block and maybe pointers to the next/prev blocks, but there are various other possible implementations which are commonly found.

Paul R