The malloc() and calloc() functions are used to dynamically allocate memory from the heap during program execution.
- malloc() allocates memory without initializing it, while calloc() allocates memory and initializes it to zero.
- Both functions return a pointer to the allocated memory, or NULL if the allocation fails.
malloc()
The malloc() function dynamically allocates a specified amount of memory during runtime.
- Allocates a single block of memory of the given size (in bytes).
- Does not initialize the allocated memory, so it contains garbage values.
- Returns a pointer to the allocated memory, or NULL if allocation fails.
calloc()
The calloc() function dynamically allocates memory and initializes it to zero.
- Allocates multiple memory blocks of the same size.
- Initializes all allocated bytes to 0.
- Returns a pointer to the allocated memory, or NULL if allocation fails.
Example: Program to demonstrates the difference between calloc and malloc
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Both of these allocate the same number of bytes,
// which is the amount of bytes that is required to
// store 5 int values.
// The memory allocated by calloc will be
// zero-initialized, but the memory allocated with
// malloc will be uninitialized so reading it would be
// undefined behavior.
int* allocated_with_malloc = malloc(5 * sizeof(int));
int* allocated_with_calloc = calloc(5, sizeof(int));
// As you can see, all of the values are initialized to
// zero.
printf("Values of allocated_with_calloc: ");
for (size_t i = 0; i < 5; ++i) {
printf("%d ", allocated_with_calloc[i]);
}
putchar('\n');
// This malloc requests 1 terabyte of dynamic memory,
// which is unavailable in this case, and so the
// allocation fails and returns NULL.
int* failed_malloc = malloc(1000000000000);
if (failed_malloc == NULL) {
printf("The allocation failed, the value of "
"failed_malloc is: %p",
(void*)failed_malloc);
}
// Remember to always free dynamically allocated memory.
free(allocated_with_malloc);
free(allocated_with_calloc);
}
Output
Values of allocated_with_calloc: 0 0 0 0 0 The allocation failed, the value of failed_malloc is: (nil)
malloc() Vs calloc() in C
Understanding the difference between malloc() and calloc() is key to dynamic memory allocation.
malloc() | calloc() |
|---|---|
| Creates a single block of memory of the specified size. | Allocates multiple contiguous blocks of memory. |
| Takes one argument: the total size (in bytes). | Takes two arguments: the number of blocks and the size of each block. |
| Generally faster because it does not initialize memory. | Slightly slower because it initializes all memory to zero. |
| Has better time efficiency due to no initialization. | Has lower time efficiency because of zero initialization. |
| Used for dynamic memory allocation. | Used for contiguous dynamic memory allocation. |
Syntax: void* malloc(size_t size); | Syntax: void* calloc(size_t num, size_t size); |
| Allocated memory contains garbage values. | Allocated memory is initialized to 0. |
| Does not have initialization overhead. | Has additional overhead for zero initialization. |