The ``++malloc++``, ``++realloc++``, ``++calloc++`` and ``++free++`` routines are used to dynamically allocate memory in the heap. But, in contrast to the ``++new++`` and ``++delete++`` operators introduced in {cpp}, they allocate raw memory, which is not type-safe, and they do not correctly invoke object constructors. Additionally, mixing them with ``++new++``/``++delete++`` results in undefined behavior.


Note that directly replacing those functions with ``++new++``/``++delete++`` is usually not a good idea (see S5025).


== Noncompliant Code Example

----
string* pStringArray1 = static_cast<string*>(malloc(10 * sizeof(string))); // Noncompliant
Person *p = (Person*)malloc(sizeof(Person)); // Noncompliant
----


== Compliant Solution

----
std::array<string, 10> stringArray1 ; // Compliant, use std::vector instead if the size is dynamic
auto p1 = new Person("Bjarne"); // Compliant, but don't do that, prefer the version on next line
auto p2 = std::make_unique<Person>("Bjarne"); // Compliant
----


== See

* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#r10-avoid-malloc-and-free[{cpp} Core Guidelines R.10] - Avoid malloc() and free()



ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
