File: no-malloc.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (48 lines) | stat: -rw-r--r-- 1,623 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
.. title:: clang-tidy - cppcoreguidelines-no-malloc

cppcoreguidelines-no-malloc
===========================

This check handles C-Style memory management using ``malloc()``, ``realloc()``,
``calloc()`` and ``free()``. It warns about its use and tries to suggest the use
of an appropriate RAII object.
Furthermore, it can be configured to check against a user-specified list of functions
that are used for memory management (e.g. ``posix_memalign()``).

This check implements `R.10
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-mallocfree>`_
from the C++ Core Guidelines.

There is no attempt made to provide fix-it hints, since manual resource
management isn't easily transformed automatically into RAII.

.. code-block:: c++

  // Warns each of the following lines.
  // Containers like std::vector or std::string should be used.
  char* some_string = (char*) malloc(sizeof(char) * 20);
  char* some_string = (char*) realloc(sizeof(char) * 30);
  free(some_string);

  int* int_array = (int*) calloc(30, sizeof(int));

  // Rather use a smartpointer or stack variable.
  struct some_struct* s = (struct some_struct*) malloc(sizeof(struct some_struct));

Options
-------

.. option:: Allocations

   Semicolon-separated list of fully qualified names of memory allocation functions.
   Defaults to ``::malloc;::calloc``.

.. option:: Deallocations

   Semicolon-separated list of fully qualified names of memory allocation functions.
   Defaults to ``::free``.

.. option:: Reallocations

   Semicolon-separated list of fully qualified names of memory allocation functions.
   Defaults to ``::realloc``.