Dynamic Memory
Allocation
Ms.M.Sangeetha,
AP(SRG)/CSE,
KEC
Static Memory Allocation
int a[9];
Dynamic Memory Allocation
The process of allocating memory at runtime
Static vs Dynamic memory allocation
Static Memory Allocation Dynamic Memory Allocation
Memory is allocated at compile
Memory is allocated at run time.
time.
Memory can't be increased while Memory can be increased while
executing program. executing program.
Used in array. Used in linked list.
Library Functions
1. malloc()
2. calloc() <stdlib.h>
3. free()
4. realloc()
malloc()
malloc() function stands for memory allocation
Allocates single block of memory with
requested size and returns the pointer (of
type void) to the first byte of allocated space.
Returns NULL if memory is not sufficient.
malloc()
Syntax
ptr = (cast_type *) malloc (byte_size);
Example
int *x;
x = (int*)malloc(5 * sizeof(int));
#include <stdio.h> #include <stdlib.h>
int main(){
int* ptr; int n, i;
n = 5;
ptr = (int*)malloc(n *sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for (i = 0; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
return 0;}
calloc()
calloc() function stands for contiguous allocation
Allocates the space for elements of an array.
Initializes the elements to zero and returns a
pointer to the memory.
Returns NULL if memory is not sufficient.
calloc()
Syntax
ptr = (cast_type *) calloc (number,byte-size);
Example
ptr = (int *) calloc(5, sizeof(int));
#include <stdio.h> #include <stdlib.h>
int main(){
int* ptr; int n, i;
n = 5;
ptr = (int*)calloc(n , sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for (i = 0; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
return 0;}
malloc() vs calloc()
malloc() function allocates memory and
leaves the memory uninitialized
calloc() function allocates memory and
initializes all bits to zero
free()
Frees or empties the previously allocated
memory space.
Syntax
free(ptr);
#include <stdio.h> #include <stdlib.h>
int main(){
int* ptr; int n, i;
n = 5;
ptr = (int*)calloc(n , sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for (i = 0; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
free(ptr);
return 0;}
realloc()
Modify the size of previously allocated
memory space
◦ If memory is not sufficient for malloc() or calloc(),
the memory can be reallocated by realloc() function
realloc()
Syntax
ptr=realloc(ptr, new-size)
Example
int *ptr;
ptr = (int*)malloc(50 * sizeof(int));
ptr = (int *) realloc(ptr, 60*sizeof(int));
Example
#include <stdio.h>
int main () {
char *ptr;
ptr = (char *) malloc(10);
strcpy(ptr, "Programming");
printf(" %s, Address = %u\n", ptr, ptr);
ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
strcat(ptr, " In 'C'");
printf(" %s, Address = %u\n", ptr, ptr);
free(ptr);
return 0;
}
Thank
You