The malloc() function dynamically allocates a specified number of bytes at runtime and returns a pointer to the beginning of the allocated memory. It is defined in the <cstdlib> header and allocates memory from the heap.
- Allocates uninitialized memory during program execution.
- Returns a void* pointer that can be cast to the required type.
Syntax
The malloc() function allocates a block of memory of the specified size in bytes.
pointer = (cast_type*) malloc(size);
Where:
- pointer stores the address of the allocated memory.
- cast_type specifies the desired pointer type.
- size is the number of bytes to allocate.

Return Value
The return value of malloc() depends on whether memory allocation succeeds.
- Returns a pointer to the allocated memory on success.
- Returns nullptr (or NULL in older code) if memory allocation fails.
Note: The allocated memory is not initialized. It contains indeterminate values until explicitly initialized.
Allocating Memory Using malloc()
The malloc() function allocates a block of memory of the specified size and returns a pointer to the beginning of that memory block. Since the allocated memory is uninitialized, its values must be assigned before use.
Example: The following program allocates memory for a single integer using malloc(), initializes it, and then prints its value.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int* ptr = (int*)malloc(sizeof(int));
if (ptr == nullptr) {
cout << "Memory allocation failed";
return 0;
}
*ptr = 25;
cout << "Value: " << *ptr << endl;
free(ptr);
return 0;
}
Output
Value: 25
Explanation
- malloc(sizeof(int)) allocates memory for one integer.
- The returned void* pointer is cast to int*.
- The allocated memory is initialized manually.
- free() releases the allocated memory.
Allocating Arrays Using malloc()
malloc() can also allocate memory for arrays whose size is determined at runtime.
Example: The following program dynamically allocates an integer array.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int* arr = (int*)malloc(n * sizeof(int));
if (arr == nullptr) {
cout << "Memory allocation failed";
return 0;
}
for (int i = 0; i < n; i++)
arr[i] = (i + 1) * 10;
cout << "Array elements: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
free(arr);
return 0;
}
Output
Array elements: 10 20 30 40 50
Explanation
- Memory is allocated for n integers.
- Each element is initialized after allocation.
- The allocated memory is released using free().
Releasing Memory Using free()
Memory allocated using malloc() must be released using free() when it is no longer required.
- Prevents memory leaks.
- Should only be used with memory allocated by malloc(), calloc(), or realloc().
Syntax
free(ptr);
Common Uses of malloc()
malloc() is commonly used when memory requirements are determined during program execution.
- Allocating memory whose size is known only at runtime.
- Implementing dynamic data structures such as linked lists, stacks, queues, and trees.
- Managing heap memory manually in low-level programs.
Related Article: malloc() vs new