49 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-no-malloc2 3cppcoreguidelines-no-malloc4===========================5 6This check handles C-Style memory management using ``malloc()``, ``realloc()``,7``calloc()`` and ``free()``. It warns about its use and tries to suggest the8use of an appropriate RAII object.9Furthermore, it can be configured to check against a user-specified list of10functions that are used for memory management (e.g. ``posix_memalign()``).11 12This check implements `R.1013<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rr-mallocfree>`_14from the C++ Core Guidelines.15 16There is no attempt made to provide fix-it hints, since manual resource17management isn't easily transformed automatically into RAII.18 19.. code-block:: c++20 21 // Warns each of the following lines.22 // Containers like std::vector or std::string should be used.23 char* some_string = (char*) malloc(sizeof(char) * 20);24 char* some_string = (char*) realloc(sizeof(char) * 30);25 free(some_string);26 27 int* int_array = (int*) calloc(30, sizeof(int));28 29 // Rather use a smartpointer or stack variable.30 struct some_struct* s = (struct some_struct*) malloc(sizeof(struct some_struct));31 32Options33-------34 35.. option:: Allocations36 37 Semicolon-separated list of fully qualified names of memory allocation functions.38 Defaults to `::malloc;::calloc`.39 40.. option:: Deallocations41 42 Semicolon-separated list of fully qualified names of memory allocation functions.43 Defaults to `::free`.44 45.. option:: Reallocations46 47 Semicolon-separated list of fully qualified names of memory allocation functions.48 Defaults to `::realloc`.49