DYNAMIC MEMORY ALLOCATION
In an array, it is must to declare the size of the array. There are two possibilities for declaring the array size. The first case is, the records that are stored are less than the size of the array, second case we want to store more records than the size of the array.
In first case, there is wastage of memory. In second case, we
can’t store more than the size of the array. Many languages
permit a programmer to specify an array’s size at runtime. Such
languages have the ability to calculate and assign, during
execution, the memory space required by the variables in a
program.
"The process of allocating memory at runtime is
called dynamic memory allocation."
There are four library
routines known as "memory management functions" that can be used
for allocating and freeing memory during program execution.
MEMORY ALLOCATION FUNCTIONS
Malloc
allocates requested size of bytes and
returns a pointer to the first byte of the allocated space.
calloc
allocates space for an array of elements,
initializes them to zero and then returns a pointer to the
memory.
Free
frees previously allocated
space.
Realloc
modifies the size of
previously allocated space.
PERMANENT STORAGE AREA:
The program instructions, global and static variables are stored in this region.
STACK:
Local variables are stored in this area.
HEAP:
The memory space that is located between these two regions is
available for dynamic allocation during execution of program.
This free memory is called the heap. The size of heap keeps
changing when the program is executed due to creation and death
of variables that are local to functions and blocks. There fore
it is possible to encounter memory ‘overflow’ during dynamic
allocation process.
Note: When the above memory
allocation functions fail to locate enough memory requested they
return a null pointer.
MALLOC:
A block of memory may be allocated using the function malloc.
The malloc function reserves a block of memory of specified size
and returns a pointer of void type.
Declaration of this
function:
Void *malloc(size_t size);
Since this
function return type is a void pointer, we can assign it to any
type of pointer. It takes the following form:
ptr=(cast_type*)malloc(size_t size);
‘ptr’ is a pointer of
type cast_type. The malloc returns a pointer (of cast type) to
an area of memory with size specified.
EX:
int
*p;
p=(int *)malloc(5*sizeof(int));
‘5’ locations of
integer size is allocated and base address is stored in p.
char *ch;
ch=(char *)malloc(10);
It allocates 10 bytes
of space for pointer ‘ch’ of type char and base address is
stored in ch.
Note: the storage space allocated
dynamically has no name and therefore its contents can be
accessed only through a pointer.
We should check whether the
allocation is successful before using the memory pointer.
PROGRAM:
Write a program to create a dynamic array and store value
into array and display.
#include<stdio.h> #include<conio.h> void main() { int *a,n,i; clrscr(); printf("enter no. of elements "); scanf("%d",&n); a=(int *)malloc(n*sizeof(int t)); printf("enter the elements \n"); for(i=0;i<n;i++) { scanf("%d",a+i); } printf("given elements"); for(i=0;i<n;i++) { printf("%d \t",*(a+i)) } getch(); }
O/P:
Enter no. of elements 5
Enter
the elements 1 2
3 4 5
Given elements
1 2 3 4 5
CALLOC
Calloc is another memory allocation function used for requesting
memory space at run time for storing derived data types such as
arrays and structures. While malloc allocates a single block of
storage space, calloc allocates multiple blocks of storage, each
of same size and then sets all bytes to zero.
Declaration
of this function:
Void * calloc(size_t n items, size_t
size);
Since this function return type is a void
pointer, we can assign it to any type of pointer. It takes the
following form:
Ptr=(cast_type *)calloc(size_t n items,
size_t size);
The above statement allocates contiguous
space for n blocks, each of size specified. All the bytes are
initialized to zero and a pointer to the first byte of the
allocated region is returned. If there is not enough space, a
NULL pointer is returned.
PROGRAM:
Write
a c program to create dynamic character array and store the
characters in the array and display.
#include<alloc.h> void main() { char *ch,n,i; clrscr(); printf("enter no. of characters"); scanf("%d",&n); ch=(char *)calloc(n,sizeof(char )); printf("enter characters"); for(i=0;i<n;i++) { scanf("%c",ch+i); } printf("given characters or string is:"); for(i=0;i<n;i++) printf("%c",ch+i); getch(); }
O/P:
Enter no. of characters: 5
Enter
characters: HELLO
Given characters or string is HELLO.
Releasing the used space:
When we no longer need the data we stored in a block of memory
and we donot intend use that block for storing any other
information, we may release that block o memory for future use,
using the "free" function.
free(ptr);
ptr--> it is a pointer to a memory block which has already
been created by malloc or calloc.
Alternating the size of a block:
When previously allocated memory is not sufficient we need
additional space for more elements. It is also possible that the
memory allocated is much larger than necessary and we want to
reduce it. In both the cases, we can change the memory size
already allocated with the function realloc. This process is
called reallocation of memory.
Syntax:
void
*realloc(void *block, size_t size);
EX:
int
*ptr;
ptr=malloc(size);
Then reallocation of space may
be done by statement
ptr=realloc(ptr, new size);