The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime.
1. Static Memory Allocation
- Definition: Memory is allocated at compile time.
- Key Characteristics:
- Fixed Size: Memory can’t be increased while executing the program.
- Usage: Commonly used in arrays.
2. Dynamic Memory Allocation
- Definition: Memory is allocated at run time.
- Key Characteristics:
- Flexible Size: Memory can be increased while executing the program.
- Usage: Primarily used in data structures like linked lists.
Malloc() Function In C
malloc() is a C function used for dynamic memory allocation. It allocates a block of memory of a specified size in bytes. The function returns a pointer to the allocated memory, or NULL
if the allocation fails.
Syntax:
void *malloc(size_t size);
size_t
: A data type used to represent unsigned integers.size
: The number of bytes to be allocated.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Allocate memory for an array of n integers
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!n");
return 1;
}
// ... (use the allocated memory)
// Free the allocated memory
free(ptr);
return 0;
}
In this example, malloc
is used to allocate memory for an array of n
integers. The allocated memory is accessed using the pointer ptr
. It’s important to check the return value of malloc
to ensure successful allocation and handle errors appropriately.
Calloc() Function In C
calloc() is another C function used for dynamic memory allocation. It’s similar to malloc
but has two key differences:
- Initialization:
calloc
initializes the allocated memory to zero. - Multiple Blocks:
calloc
allocates multiple blocks of memory of the same size.
Syntax:
void *calloc(size_t num, size_t size);
num
: The number of elements to allocate.size
: The size of each element in bytes.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Allocate memory for an array of n integers and initialize to zero
ptr = (int *)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!n");
return 1;
}
// ... (use the allocated memory)
// Free the allocated memory
free(ptr);
return 0;
}
In this example, calloc
is used to allocate memory for an array of n
integers and automatically initializes all elements to zero. This can be useful when working with arrays that need to be initialized to specific values.
Realloc() Function In C
realloc() is a C function used to dynamically resize a previously allocated block of memory. It takes a pointer to the existing memory block and a new size as input. If the new size is larger than the original size, the function attempts to allocate a new block of memory of the requested size and copies the contents of the original block to the new block. If the new size is smaller, the function attempts to shrink the existing block to the requested size.
Syntax:
void *realloc(void *ptr, size_t new_size);
ptr
: A pointer to the existing memory block.new_size
: The new size of the memory block in bytes.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n, m;
printf("Enter the initial number of elements: ");
scanf("%d", &n);
// Allocate memory for an array of n integers
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!n");
return 1;
}
// ... (use the allocated memory)
printf("Enter the new number of elements: ");
scanf("%d", &m);
// Resize the memory block
ptr = (int *)realloc(ptr, m * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed!n");
return 1;
}
// ... (use the resized memory)
// Free the allocated memory
free(ptr);
return 0;
}
In this example, realloc
is used to resize the memory block pointed to by ptr
to accommodate m
elements. If the new size is larger, realloc
will attempt to allocate a new block of memory and copy the contents of the original block. If the new size is smaller, realloc
will attempt to shrink the existing block.
Free() Function In C
free() is a C function used to deallocate memory previously allocated using malloc
, calloc
, or realloc
. It takes a pointer to the memory block to be freed as its argument.
Syntax:
void free(void *ptr);
ptr
: A pointer to the memory block to be freed.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Allocate memory for an array of n integers
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!n");
return 1;
}
// ... (use the allocated memory)
// Free the allocated memory
free(ptr);
return 0;
}
In this example, free()
is used to deallocate the memory block pointed to by ptr
after it is no longer needed. Failing to free allocated memory can lead to memory leaks, which can cause performance issues and program instability.
Key Points
- Pointer: Dynamic memory allocation returns a pointer to the allocated memory.
- Memory Deallocation: Always use
free()
to deallocate memory that is no longer needed to prevent memory leaks. - Error Handling: Check the return value of
malloc
andcalloc
to ensure successful allocation. realloc
: Userealloc()
to resize an existing memory block.
Best Practices
- Allocate only the necessary memory.
- Use
free()
to deallocate memory promptly. - Check for allocation failures and handle errors gracefully.
- Consider using
calloc
for zero-initialized memory. - Be mindful of memory leaks.