Deallocation of memory

When we allocates the memory using malloc() and calloc(), it returns pointer pointing to the base address to the allocated memory. But while freeing the memory we call free(pointer) then

How the operation system knows that how much memory is about to be free.

Questions by sanjayojha02   answers by sanjayojha02

Showing Answers 1 - 9 of 9 Answers

dipuprits

  • Apr 14th, 2009
 

I have a logical explanation to this but anybody is free to correct me. Here is what I think.

As we know, the memory structure is a link list so each block of memory in a group of blocks is linked to each other.

When we call malloc() or calloc(), we pass the information of how much memory needs to be allocated like

 Ex: malloc(sizeof(int) * 100).

Internally the OS finds free blocks of memory (depending on what is the sizeof(int) in that OS) that can be used and links them to each other marking the start and end sentinels. The starting address of this link list is returned for the calling program to use.

When a free(p) is called, only the starting address needs to be known. Once this is provided, the link list is run through and memory is marked as free or deallocated. These blocks can now be used by the OS for other memory allocations.

A delete(p), of course, calls the destructor before doing this.

The OS will have its own safty measures so that a program does not iterate through this link list as this is hidden to the outside world.

This also explains the reason a defragmented system is faster in responding to memory allocations than a fragmented system as the OS has to probably search for a free block.

tewari2312

  • Jun 13th, 2009
 

Should have used the term compiler rather than OS, when we use these functions a pointer is formed pointing to the location of the base address of the allocated memory and when the value is assigned at the allocated memory an pointer is created implicetly that points to the location of the last address of the allocated memory.

  Was this answer useful?  Yes

Dynamic memory is allocated on heap. De-allocation is counterpart of the
allocation operation. the task of allocation and De-allocation is handled by the
base/standard libraries. These store information about the allocated memory like
base address and size. initially all of heap is free.


Many implementations are possible. e.g the free space may be maintained in a
free list and the the allocated memory in a doubly linked list or hash table.


Memory can only be requested to be de-allocated with the same address which
was returned during the allocation.


Fragmentation of the free space is a known problem. a request for allocation
may fail even when there is enough memory available in the heap. it may be due
to the non-availability of a contiguous chunk of memory at least equal to the
size of the requested size.


Automatic heap management strategy e.g. garbage collection alleviate some of
the problems with better utilization of the heap memory.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions