Dynamic memory
allocation in C:
The process of allocating memory during program
execution is called dynamic memory allocation.
This is use full in the situations when the size of array we
declare initially is fixed and cannot be modified . Dynamic memory allocation
allows a program to obtain variable amount of memory space, while running or to
release space when no space is required.
Dynamic memory
allocation functions in C:
C
language offers 4 dynamic memory allocation functions. They are library functions under "stdlib.h".
- malloc()
- calloc()
- realloc()
- free()
S.no
|
Function
|
Syntax
|
1
|
malloc ()
|
Allocates requested size
of bytes and returns a pointer first byte of allocated space
Syntax : ptr=(cast-type*)malloc(byte-size) Here, ptr is pointer of cast-type. Example : ptr=(int*)malloc(100*sizeof(int)); |
2
|
calloc ()
|
Allocates space for an array elements, initializes to zero and
then returns a pointer to memory
Syntax : ptr=(cast-type*)calloc(n, element-size); Here, ptr is pointer of cast-type. Example :ptr=(float*)calloc(25, sizeof(float)); |
3
|
realloc ()
|
Change the size of previously allocated space
Syntax : ptr=realloc(ptr-name, newsize); Here, ptr is pointer of cast-type. Example :ptr=realloc(ptr, 40); |
4
|
ree ()
|
deallocate the previously
allocated space
Syntax : free (pointer_name);
Example :free(ptr); Here, ptr is pointer to the memory that is dynamically created.. |
1. malloc() function in
C:
- malloc () function is used to
allocate single block of space in memory during the execution of the
program.
- malloc () does not initialize
the memory allocated during execution. It carries garbage value.
- malloc () function returns a
pointer if it has created a memory otherwise returns a null pointer if it
couldn’t allocate requested amount of memory.
· Syntax : ptr=(cast-type*)malloc(byte-size)
Here, ptr is pointer of cast-type.
· Example : ptr=(int*)malloc(100*sizeof(int));
If the size of int is 2 bytes The statement will allocate 200 and the pointer points to the address of first byte of memory.
2. calloc() function in
C:
·
calloc() allocates multiple
blocks of memory each of same size and sets all bytes to zero.
- calloc () function returns a
pointer if it has created a memory otherwise returns a null pointer if it
couldn’t allocate requested amount of memory.
· Syntax : ptr=(cast-type*)calloc(n, element-size);
Here, ptr is pointer of cast-type.
· Example :ptr=(float*)calloc(25, sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.
3. realloc() function in
C:
- realloc () function modifies
the allocated memory size by malloc () and calloc () functions to new
size.
- If enough space doesn’t exist
in memory of current block to extend, new block is allocated for the full
size of reallocation, then copies the existing data to new block and then
frees the old block.
· Syntax : ptr=realloc(ptr-name, newsize);
Here, ptr is pointer of cast-type.
- Example : ptr=(int*)malloc(100*sizeof(int));
If the size of int is 2 bytes The statement will allocate 200 and the pointer points to the address of first byte of memory.
ptr=realloc(ptr, 150* sizeof(int));
The statement will reallocate memory of 300 bytes and returns pointer points to the address of first byte of memory otherwise returns a null pointer if it couldn’t allocate requested amount of memory.
4. free() function
in C:
- free () function frees the
allocated memory by malloc (), calloc (), realloc () functions and returns
the memory to the system.
·
Syntax : free
(pointer_name);
· Example : free(ptr);
Here, ptr is pointer to the memory that is dynamically created..
Example program for
malloc() function in C:
#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = (char*)malloc( 20 *
sizeof(char) );
if( mem_allocation== NULL )
{
printf("Couldn't able to allocate
requested memory\n");
}
else
{
strcpy(
mem_allocation,"fresh2refresh.com");
}
printf("Dynamically allocated memory
content : " \
"%s\n", mem_allocation );
free(mem_allocation);
}
Example program for
calloc() function in C:
#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = calloc( 20, sizeof(char)
);
if( mem_allocation== NULL )
{
printf("Couldn't able to allocate
requested memory\n");
}
else
{
strcpy(
mem_allocation,"fresh2refresh.com");
}
printf("Dynamically allocated memory
content : " \
"%s\n",
mem_allocation );
free(mem_allocation);
}
Example program for
realloc() and free() functions in C:
#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = malloc( 20 * sizeof(char)
);
if( mem_allocation == NULL )
{
printf("Couldn't able to allocate
requested memory\n");
}
else
{
strcpy( mem_allocation,"fresh2refresh.com");
}
printf("Dynamically allocated memory
content : " \
"%s\n", mem_allocation );
mem_allocation=realloc(mem_allocation,100*sizeof(char));
if( mem_allocation == NULL )
{
printf("Couldn't able to allocate
requested memory\n");
}
else
{
strcpy( mem_allocation,"space is
extended upto " \
"100
characters");
}
printf("Resized memory : %s\n",
mem_allocation );
free(mem_allocation);
}
No comments:
Post a Comment