In C++, malloc and free are two obsolete functions used for dynamic memory management (i.e., for reserving or freeing space at runtime).
These two functions are inherited from C. These functions are available in C++ to maintain compatibility with C.
However, their use in modern C++ programs is discouraged. Instead, in C++, dynamic memory management should be performed with new and delete.
Do not use malloc and free
I’m telling you about them in this article because you will encounter them in code out there.
But in C++, you should use new and delete.
What is malloc
The malloc function (memory allocation) is used to reserve a block of dynamic memory of a specific size.
When using it, memory is reserved in the heap, and the address of the allocated block is returned as a void* pointer.
#include <cstdlib> // Necessary for malloc
void* malloc(size_t size);
size: The amount of memory to allocate (in bytes)void*: A generic pointer that is returned (we must cast it to the desired pointer type).
If the allocation fails, it returns nullptr.
Let’s see the use of malloc more clearly with an example,
// Allocate memory for an integer
int* p = (int*)malloc(sizeof(int));
if (p != nullptr) {
*p = 10; // Initialize the integer
std::cout << *p << std::endl; // Print the value
}
In this example,
- Memory is allocated for an integer.
- To get the size in bytes of an
inttype we usesizeof. - We have to perform a cast of the returned pointer to the
int*type, becausemallocreturns a pointer of typevoid*.
What is free
The free function is used to free memory that was previously reserved with malloc (the freed memory can be reused for future allocations)
void free(void* ptr);
Where,
ptr: A pointer to the memory to be freed.
Let’s see it with an example,
// Allocate memory
int* p = (int*)malloc(sizeof(int));
if (p != nullptr) {
free(p); // Free the memory
}
In this example,
- Memory is allocated for an integer
- We free it immediately (not very useful, but it’s an example).
If memory is not freed after use, a memory leak occurs. That is, your program consumes more and more resources by using them poorly (and it’s sloppy)
Disadvantages and Problems
We have already said that in C++ you should not use malloc and free (except when you have legacy C code, or need to be compatible with it) because it has significant disadvantages.
Let’s look at some of them 👇
Manual Memory Management
Using malloc and free requires the programmer to manage memory manually, which can lead to errors such as:
- Memory Leaks: Not freeing memory that is no longer needed.
- Double Free: Attempting to free the same memory more than once.
- Access to Freed Memory: Attempting to access memory after it has been freed.
Lack of Type Safety
One of the main disadvantages of malloc and free is that they work with void* pointers, which means explicit casting is required to convert the returned pointer to the correct data type.
Lack of Construction and Destruction
malloc and free simply allocate and free memory without calling the constructors and destructors of objects.
This means that no type-specific initializations or cleanups are performed when using these functions.
