Debian Codegraph @

llvm-project/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/no-malloc.rst

raw
1 .. title:: clang-tidy - cppcoreguidelines-no-malloc
2
3 cppcoreguidelines-no-malloc
4 ===========================
5
6 This check handles C-Style memory management using ``malloc()``, ``realloc()``,
7 ``calloc()`` and ``free()``. It warns about its use and tries to suggest the use
8 of an appropriate RAII object.
9 Furthermore, it can be configured to check against a user-specified list of functions
10 that are used for memory management (e.g. ``posix_memalign()``).
11 See `C++ Core Guidelines <https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-mallocfree>`_.
12
13 There is no attempt made to provide fix-it hints, since manual resource
14 management isn't easily transformed automatically into RAII.
15
16 .. code-block:: c++
17
18 // Warns each of the following lines.
19 // Containers like std::vector or std::string should be used.
20 char* some_string = (char*) malloc(sizeof(char) * 20);
21 char* some_string = (char*) realloc(sizeof(char) * 30);
22 free(some_string);
23
24 int* int_array = (int*) calloc(30, sizeof(int));
25
26 // Rather use a smartpointer or stack variable.
27 struct some_struct* s = (struct some_struct*) malloc(sizeof(struct some_struct));
28
29 Options
30 -------
31
32 .. option:: Allocations
33
34 Semicolon-separated list of fully qualified names of memory allocation functions.
35 Defaults to ``::malloc;::calloc``.
36
37 .. option:: Deallocations
38
39 Semicolon-separated list of fully qualified names of memory allocation functions.
40 Defaults to ``::free``.
41
42 .. option:: Reallocations
43
44 Semicolon-separated list of fully qualified names of memory allocation functions.
45 Defaults to ``::realloc``.