Dynamic Memory Management in
Data Structures
Memory management
The responsibility of an operating
system is not constrained in process management but the OS should efficiently
manage its primary memory. In the operating system this responsibility is
carried out by memory manager which is a part of an operating system. Since for each
process to run, there must be a certain amount of primary memory, the memory
manager's performance is crucial to the entire system's performance. The memory
manager allocates space in a primary memory for processes and the same loads
and stores the contents in the primary memory. The memory manager also assists
the programmer. The memory manager's basic tasks are controlling the
primary memory partition and minimizing memory access time.
In the case of system multiple processes
are running simultaneously. The real challenge is that we need to manage the
memory effectively. Memory multiplexing is possible in primary memory. So the
memory manager can allocate each process some primary memory for its own use. However, as new
processes are generated and old processes are finished, the memory manager can
control which processes operate on which memory locations, and also decide how
to assign and override available memory. Although different techniques are used
to assign space for competing memory procedures, best fit, worst fit and first
fit are the three most common.
First fit
There may be several gaps in the memory,
so from the first hole it finds which is large enough to occupy the process,
the operating system assigns memory, starting from the beginning of the primary
storage, to reduce the time it takes to evaluate the available space to satisfy
the request. The
first free partition or hole wide enough to fulfil the process is allocated in
the first fit strategy. After locating the first appropriate free partition, it
terminates.
For example if a process needs 5KB of
space. The memory manager include a list of 4KB, 20KB, 15KB, 10KB and 6KB. Here
in this mechanism, memory manager will allocate the first free memory 20KB is
allotted for this process
Advantage: It is the fastest algorithm among the different memory management techniques. Because its searching condition is very easy.
Disadvantage: If it is too tiny, the remaining unused memory regions available after allocation become garbage. Therefore, the need for greater memory requirements cannot be fulfilled.
Best fit allocation :
An object can be positioned by the
allocator in the smallest amount of unallocated memory.
To satisfy the needs of
the application process, the best approach is to assign as little free
partition as possible. The whole list of free partitions is first searched by
this algorithm and the hole is considered small enough. It then tries to find a
gap which approximates the necessary actual process size.
The process needs 10 KB of memory and
currently the memory manager includes a list of 9 KB, 17 KB, 20 KB, 11 KB and
12 KB unallocated volume blocks. The best fit strategy allocates 10 KB to the
11 KB volume process.
Advantage: Memory use is better than the first fit because it looks first for the smallest free partition available.
Inconveniences: It is slow and the memory may be filled by small useless holes.
Worst fit:
A process is placed into the largest
packet of unassigned memory by the memory manager. The premise is that,
relative to the best fit, this space produces the largest space after the
allotment, where the remaining space can be used for another process. It is the
reverse of the best fit. In this approach memory manager checks the whole
available memory partitions and finds the largest free space and allocates the
memory for that process.
For example if a process needs 10KB of
space. The available spaces are 20KB, 15KB, 5KB, 10KB and 30KB. In this
approach the memory manager will assign 30KB partition to that process.
Advantages : Reduces the production capacity for tiny
holes.
Disadvantage : If a procedure that needs additional
memory comes to a later level, since the greatest hole is already split and
filled, it cannot be accepted.
Dynamic memory allocations
The dynamic memory allocation principle
in the c language makes it possible for the C developer to allocate memory at
running time. The 4 functions of the stdlib.h header file allow dynamic memory
allocation in the c language. In C language we can execute a program in which
we cannot know the sequential size of the declared variable until the run time.
The process is termed as dynamic memory allocation.
Manual allocation and freeing up of
memory according to the programming needs is dynamic memory allocation. Dynamic
memory is handled and references are provided to show the newly allocated
memory space in the field called the heap. Here we can construct and delete
consecutive elements at the execution time without any interruption. This is
done with the help of Automatic Memory Manager layer and C dynamic memory
allocation stack.
<stdlib.h> is the library which is
responsible for Dynamic Memory Management. Now we are discussing about some
library functions which can be used in Dynamic Memory allocation.
malloc()
The function C malloc() stands for
memory allocation. It is a function that is used dynamically to assign a memory
space. It preserves the prescribed size of the memory space and returns a null
pointer pointing to the address of the memory. The returned pointer is
typically of the void type. This allocates the memory of requested size and
returns the pointer to the first byte of allocated space.
Syntax of malloc()
ptr = (cast_type *) malloc (byte_size);
ptr is the name of the pointer of cast
type. Here the function returns a pointer to the allocated memory of byte_size.
Consider the below code:
ptr = (int *) malloc (50)
When this code is executed without any
error, 75 bytes of memory will be allocated. The
index of the address type int of the first byte of the allotted space is
assigned to ptr.
calloc()
To assign a given amount of memory and
then initialize it to zero, the calloc() function in C is used. The function
returns this memory location with a void pointer, which can then be cast to the
desired form. Two parameters that collectively determine the amount of memory
to be allocated are used for the function. This allocates the space for
elements of an array. Initializes the elements to zero and returns a pointer to
the memory.
Syntax of calloc() function
ptr = (type *) calloc (n, size);
When this statement is executed, n
memory blocks of same size is allocated. Once the memory block is allocated all
the bytes are allocated to zero. The pointer which is pointing at the first
byte of the allocated memory is returned. If there is any interruption such as
scarcity of memory, then we get a null pointer.
The major differences between malloc()
and calloc() functions
In general, the calloc() function is
more suitable and effective than the malloc() function. Calloc() will allocate
several volumes at once when both functions are used to allocate memory space.
You don't need to ask each time for a block of memory. The Malloc() function generates a single
memory cell of the user's defined size. For a variable, the Calloc() function
may assign various memory fragments. The Malloc function includes the value
of garbage. The
memory block assigned by the calloc function is always zero-initialized. Calloc is slower
than malloc. calloc is more secure than malloc.
realloc()
It is used to modify the size of
previously allocated memory space.. Realloc() is a C library feature for
attaching more storage space to memory blocks that are already allocated. The
aim of realloc in C is to extend current memory blocks while keeping the
original information as it is. The function realloc() helps to minimize malloc
or calloc functions' size of already allocated memory. Realloc stands for
memory allocation.
Syntax of realloc()
ptr = realloc (ptr,nsize);
The above statement assigns new memory space with a certain amount of variable nsize. After running the function, the pointer returns to the first byte of the memory module. The new size may be larger or smaller than the previous memory. We cannot be sure whether the newly allocated block will point to the same location as the previous memory block. This function copies all previous data to the new region. This ensures that the data is secure.
free()
Frees or empties the previously
allocated memory space. The free() function is used to de-allocate the storage
assigned by the malloc(), calloc(), etc functions and return it to the heap
function so that it can be used for other applications. The free () function
argument is the pointer to the memory to be released.
Syntax of free() function
void free(void *pntr)
Here pntr is the address which is
pointing to the previously allocated memory block by malloc(), calloc() or
realloc(). When this code was executed the memory pointed by the pntr is
deallocated. If the value of pntr is a null value then no action will occur.