Tuesday 30 July 2013

Heap Memory concepts in C

Heap Memory

è Also called as dynamic memory, is an alternative to the local stack memory
Heap Memory
Local stack Memory
Manual allocation and deallocation
Programmer explicitly request the size of the memory needed
Automatic allocation and deallocation
Allocation à when function call
Deallocation à when function exists
Programmer has much greater control of memory
The blocks of memory can be requested of any size

More work

More bugs


The heap is an area of memory available to allocate areas ("blocks") of memory for the program.

Heap Manager:

Heap memory is managed by the library code called “heap manager”, used to keep track of the free blocks (available for use) and currently used blocks by the program and size of these blocks. The entire heap is free initially. Programmer requests block of memory to the Heap Manager by using library functions like malloc(), realloc() and calloc(). Heap memory can be freed by using free().
The prototypes of these functions are available in <stdlib.h>
The memory requests (memory allocation functions) like malloc() returns NULL pointer if the heap memory is full.

void* malloc(unsigned long size);
size
à requested size of the block in bytes
Returns pointer to the newly allocated heap block if successful.
Returns NULL if heap is full

void free(void* heapBlockPointer);
Takes argument as pointer to the heap block and returns this block to free pool for later re-use.


How allocation functions works? It requests block of memory in the heap of a particular size. The heap manager chooses a free block, and marks this area as “in use” in its private data structure, and returns a pointer to this heap block. And, heap manager will not allocate the same block to another request. Once the block is allocated, it is the owner responsibility to clear the old content in that block. We have one memory allocation function (calloc() in c) which sets the allocated blocks to all zeros.
Each allocation request reserves a contiguous area of the requested size in the heap and returns a pointer to that new block to the program.

The heap manager has its own, private data structures to record what areas of the heap are committed to what purpose at any moment the heap manager satisfies each allocation request from the pool of free memory and updates its private data structures to record which areas of the heap are in use.

How deallocation works? It is opposite to the allocation function. The owner makes deallocation request to return the heap block to the heap free area. The deallocation function takes the pointer to the heap block, which is returned by the allocation function. This pointer should be the same pointer returned earlier by the allocation function.

When the program is finished using a block of memory, it makes an explicit deallocation request to indicate to the heap manager that the program is now finished with that block.
The heap manager updates its private data structures to show that the area of memory occupied by the block is free again and so may be re-used to satisfy future allocation requests.

Memory Leaks:

If programmer forgets to deallocate the allocated heap memory then the program said to have a memory leak, that leads to abnormal behavior based on the allocation requests.
It is a serious problem if the program runs indeterminate amount of time, that results in continuous heap requests but there are no deallocation requests and the heap memory becomes full.

Garbage Collector


It takes over the most of the responsibility for Heap Management at the cost of little extra time taken at runtime. The programming lang like Perl and Java has the feature called Garbage Collector.

No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...