1. Gandhinagar Institute Of Technology
Subject :-Computer Programming And Utilizaton ()
Topic :- Dynamic Memory Allocation
Branch :- Computer Engineering (CE)
Division :- B
Prepared By :- Grishma Rajput(160120107122)
Submitted To :- Prof. Mihir Shah
2. Dynamic Memory Allocation
In C, the exact size of array is unknown until compile time, i.e.,
the time when a compiler compiles your code into a computer
understandable language. So, sometimes the size of the array
can be insufficient or more than required.
Dynamic memory allocation allows your program to obtain more
memory space while running, or to release it if it's not required.
In simple terms, Dynamic memory allocation allows you to
manually handle memory space for your program.
Although, C language inherently does not have any technique to
allocate memory dynamically, there are 4 library
functions under "stdlib.h" for dynamic memory allocation.
3. Memory Allocation Functions
Function Use of Function
malloc()
Allocates requested size of bytes and returns a pointer first
byte of allocated space
calloc()
Allocates space for an array elements, initializes to zero
and then returns a pointer to memory
free() deallocate the previously allocated space
realloc() Change the size of previously allocated space
4. Memory Allocation Process
Global variables, static variabl
es and program instructions get
their memory
in permanent storage area
whereas local variables are
stored in area called Stack.
The memory space between
these two region is known
as Heap area. This region is
used for dynamic memory
allocation during execution of
the program. The size of heap
keep changing.
5. malloc()
The name malloc stands for "memory allocation".
The function malloc () reserves a block of memory of specified size and return
a pointer of type void which can be casted into pointer of any form.
Syntax of malloc ()
Here, ptr is pointer of cast-type. The malloc () function returns a pointer to an
area of memory with size of byte size. If the space is insufficient, allocation fails
and returns NULL pointer.
This statement will allocate either 200 or 400 according to size of int 2 or 4
bytes respectively and the pointer points to the address of first byte of memory
ptr = (cast-type*) malloc(byte-size)
ptr = (int*) malloc(100 * sizeof(int));
6. malloc ( ) Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main ( )
{
int*p;
clrscr( );
p= (int*) malloc(1* sizeof ( int ));
*p=10;
printf(“n Value: %d”, *p);
free(p);
getch( );
}
OUT PUT:
Value : 10
7. calloc()
The name calloc stands for "contiguous allocation".
The only difference between malloc() and calloc() is that, malloc() allocates
single block of memory whereas calloc() allocates multiple blocks of
memory each of same size and sets all bytes to zero.
Syntax of calloc()
This statement will allocate contiguous space in memory for an array
of nelements. For example:
This statement allocates contiguous space in memory for an array of 25
elements each of size of float, i.e, 4 bytes.
ptr = (float*) calloc(25, sizeof(float));
ptr = (cast-type*)calloc(n, element-size);
8. calloc ( ) Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main ( )
{
int*p;
clrscr( );
p= (int*) calloc(1, sizeof ( int ));
*p=10;
printf(“n Value: %d”, *p);
free(p);
getch( );
}
OUT PUT:
Value : 10
9. Difference between malloc() and calloc()
calloc() malloc()
calloc() initializes the allocated memory with
0 value.
malloc() initializes the allocated memory with
garbage values.
Number of arguments is 2 Number of argument is 1
Syntax :
(cast_type *)calloc(blocks , size_of_block);
Syntax :
(cast_type *)malloc(Size_in_bytes);
10. C free()
Dynamically allocated memory created with either calloc() or
malloc() doesn't get freed on its own. You must explicitly use
free() to release the space.
syntax of free()
This statement frees the space allocated in the memory pointed
by ptr.
free(ptr);
11. C realloc()
If the previously allocated memory is insufficient or more than
required, you can change the previously allocated memory
size using realloc().
Syntax of realloc()
This function allocates a new memory space of size new size
to the pointer variable ptr and returns a pointer to the first byte
of the new memory block. The new size may be larger or
smaller than the size.
If the function is unsucessfull in locating additional space, it
returns NULL pointer and the original block is freed.
ptr = realloc(ptr, newsize);
12. relloc ( ) Incorrect Eample
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{
int arr[2], i;
int *ptr = arr;
int *ptr_new;
arr[0] = 10;
arr[1] = 20;
// incorrect use of new_ptr: undefined behaviour
ptr_new = (int *) realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
getchar();
}
OUT PUT:
NULL
13. relloc( ) Correct Example
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *) malloc(sizeof(int)*2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *) realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++)
printf("%d ", *(ptr new + i));
getchar();
}
OUT PUT :
10 20 30