Valgrind and Memory Leaks

When Should I Use malloc in C?

Learn when malloc still makes sense in C++ code, how to cast its result, how to pair it with free, and how Valgrind reports mismatched allocations.

On this page
  1. The short answer
  2. What malloc does
  3. Using malloc in C++
  4. Where malloc still belongs
  5. Where malloc is the wrong tool
  6. How Valgrind reports malloc mistakes
  7. Quick rule

The short answer

In C++ code you rarely need malloc. Reach for std::vector, std::string, or a smart pointer first. Use malloc when a C library requires it, because the library returns memory you must release yourself, or when you are writing low-level code that manages raw memory on purpose. Whichever you use, every allocation needs exactly one matching release.

What malloc does

malloc asks the heap for a block of raw bytes and returns a pointer to it. You give it a size in bytes, and it returns a void*, or a null pointer if the request fails. It doesn’t know what type you will store, and it doesn’t run constructors. You release the block with free.

Using malloc in C++

C++ doesn’t convert a void* to another pointer type on its own, so the result needs a cast. The size is in bytes, so five ints need count * sizeof(int):

// malloc_demo.cpp
#include <cstddef>
#include <cstdlib>
#include <iostream>

int main() {
    std::size_t count = 5;
    int* numbers = static_cast<int*>(std::malloc(count * sizeof(int)));
    if (numbers == nullptr) {
        std::cerr << "allocation failed\n";
        return 1;
    }

    numbers[0] = 42;
    std::cout << numbers[0] << '\n';

    std::free(numbers);  // pair malloc with free, not delete
    return 0;
}

std::malloc and std::free come from <cstdlib>. The null check matters because malloc can fail.

Where malloc still belongs

Some C functions return memory that you own and must release with free. On POSIX systems, two common examples are strdup, which copies a string into new memory, and getline, which allocates its buffer for you. Calling delete on that memory is a bug, even when the code appears to work.

If you write low-level code that manages raw memory on purpose, realloc resizes a block you already own, and calloc returns zeroed memory. Both still pair with free.

Where malloc is the wrong tool

  • Class types. malloc doesn’t call constructors, and free doesn’t call destructors. For objects with real constructors, use new or a container instead.
  • Ordinary arrays. std::vector handles sizing, copying, and freeing for you.
  • Mixed release. Calling delete on malloc’d memory, or free on memory from new, is undefined behavior.

Here is the mix-up in code:

int* value = static_cast<int*>(std::malloc(sizeof(int)));
delete value;  // wrong: this memory came from malloc, so release it with free

How Valgrind reports malloc mistakes

If you never call free, Valgrind reports the block as definitely lost, the same way it reports a leaked new. If you call delete on malloc’d memory, Valgrind reports:

Mismatched free() / delete / delete []

To run Valgrind on a program like this, follow how to use valgrind in c.

Quick rule

  • Use std::vector, std::string, and std::unique_ptr by default.
  • Use malloc only where a C API requires it, and release the memory with free.
  • Never mix malloc with delete, or new with free.

The complete guide to valgrind and memory leaks

How to Use Valgrind With C++
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Stringify a Macro in C++

Learn how the # operator turns a C++ macro argument into a string literal, with examples, the two-level trick for expanded macros and common pitfalls.

Macros 3 min read