DYNAMIC MEMORY ALLOCATION
Memory allocation for variables can be done by 2
methods-
Static memory allocation/Compile –time allocation.
Dynamic memory allocation/Run-time allocation.
Static memory allocation
Static memory allocation is an allocation technique which
allocates a fixed amount of memory for variables during
compile time.
Operating system internally uses a data structure known as
STACK to manage this.
Stack memory is allocated during compilation time execution.
This is known as static memory allocation.
Eg – int A[10]={11,22,33,44,55,66,77,88,99,100}
Eg – int A[100]={1,2,3,4,5,6,7,8,9,10}
here 2x100=200 bytes of memory is allocated to array A but only 10 elements are input i.e
2x10=20 bytes is used remaining 200-20=180 bytes of memory is wastage and that memory
cannot used by program
Disadvantage of static memory allocation
We can't change the size of a variable which is allocated at
compile-time.
Less efficient.
Lots of space wastage in case of large array size.
Need of Dynamic Memory allocation
To avoid the problem of Static Memory Allocation, there is a
need of Dynamic Memory Allocation.
Dynamic Memory Allocation- Dynamic memory allocation is
an allocation technique which allocates a user required amount
of memory for variables during Run-time.
HEAP memory is allocated during Run-time execution. This is
known as Dynamic memory allocation.
Dynamic memory allocation is implemented with the help of
POINTER.
C language provides different functions to achieve runtime
memory allocation like- malloc( ), calloc( ), realloc( ) and free( ).
All these functions are available in <stdlib.h> library.
To allocate memory – (1) malloc( ) (2) calloc( )
To resize the memory – realloc( )
To delete memory- free( )
Note- Dynamic memory allocation function return Pointer to
Void type which can be TypeCast to any Data-type.
Note-The main advantage of using dynamic memory allocation
is preventing the wastage of memory.
Note- The main disadvantage of dynamic memory allocation is
that memory must be free by programmer otherwise it give bugs
which is difficult to find.
1.malloc( )
malloc( ) – malloc stands for memory allocation.
The malloc( ) function reserves a single block of memory of the
specified number of bytes and return void Pointer.
malloc( ) take single argument.
malloc( ) initialize Garbage value to the allocated memory.
Syntax
ptr = (cast_type *) malloc (byte_size);
or
ptr = (void *) malloc (byte_size);
Eg- ptr = (int*) malloc(100*sizeof(int));
A memory of 200 bytes is allocated b/c size of int is 2 bytes and
pointer ptr hold the address of first byte of allocated memory.
Note- If memory is not created than pointer ptr assign to NULL.
2. Calloc( )
calloc( ) stands for contiguous allocation.
calloc( ) is used to allocate a multiple/ n blocks of memory of
same size.
calloc( ) takes 2 arguments.
calloc( ) initializes all bytes to zero.
Syntax
ptr = (castType*)calloc(n, size);
or
ptr = (void *)calloc(n, size);
Eg- ptr = (int*) calloc(100, sizeof(int));
The above statement allocates contiguous space in memory for
100 elements of type int
3.free( )
Dynamically allocated memory created with either malloc( ) or
calloc( ) doesn't get freed on their own. At the end of program
programmer must free the pointer ptr using free( )
Syntax
free(ptr);
This statement frees the space allocated in the memory pointed
by ptr
4.realloc( )
realloc( ) stands for reallocation of memory.
Realloc( ) can add or delete the memory which is created by
malloc( ) or calloc( ) while leaving the original content as it is.
Syntax
ptr = realloc (ptr, newsize);
The above statement allocates a new memory space with a speci
fied size in the variable newsize. The new size can be larger or s
maller than the previous memory.