Language: EN

cpp-malloc-y-free

What is malloc and free in C++

In C++, malloc and free are two deprecated functions used for dynamic memory management (that is, to allocate or free 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 mention this in this article because you will encounter it in code out there.

But in C++ you should use new and delete.

What is malloc

The malloc function (memory allocation) is used to allocate a block of dynamic memory of a specific size.

When used, memory is allocated on 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: This is the amount of memory you want to allocate (in bytes)
  • void*: This is 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 int type we use sizeof.
  • We have to perform a cast of the returned pointer to int*, since malloc returns a pointer of type void*.

Dynamic array allocation

malloc can also be used to allocate memory for dynamic arrays.

// Allocates memory for an array of 5 integers
int* arr = (int*) malloc(5 * sizeof(int)); 

The amount of memory requested must be sufficient to store all the elements of the array.

What is free

The free function is used to release memory that was previously allocated with malloc (the freed memory can be reused for future allocations)

void free(void* ptr);

Where,

  • ptr: A pointer to the memory that you want to free.

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 keeps consuming more and more resources due to improper usage (and it’s a mess)

Disadvantages and problems

We’ve already said that in C++ you shouldn’t use malloc and free (except when you have legacy C code, or need to be compatible with it) because there are significant disadvantages.

Let’s see some of them 👇

Manual memory management

Using malloc and free requires the programmer to manually manage memory, which can lead to errors such as:

  • Memory Leaks: Not freeing memory that is no longer needed.
  • Double Free: Trying to free the same memory more than once.
  • Accessing Freed Memory: Trying 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 that an 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 specific initializations or cleanups are performed when using these functions.