GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 197 of 203    Print  
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.



  
Total Answers and Comments: 3 Last Update: June 17, 2009     Asked by: sanjayojha02 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: dipuprits
 

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.



Above answer was rated as good by the following members:
kvkrishnaprasad
April 14, 2009 11:09:30   #1  
dipuprits Member Since: April 2009   Contribution: 3    

RE: Deallocation of memory

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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 13, 2009 01:17:44   #2  
tewari2312 Member Since: June 2009   Contribution: 4    

RE: Deallocation of memory
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.
 
Is this answer useful? Yes | No
June 16, 2009 04:40:05   #3  
amarnathsatrawala Member Since: June 2009   Contribution: 3    

RE: Deallocation of memory

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.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape