1. Use of new without delete
Some scenarios where this is intended:
- Functions returning pointer to allocated memory. Calling function must free the memory.
- Functions assigning pointer to parameter (by pointer or reference). Calling function must free the memory.
- Functions assigning pointer to object parameter (class/struct member). Calling function must free the memory.
- Pointer is stored in class member and freed somewhere else (e.g. in destructor). The
delete
instruction may be in another source file in this case.
See the Run-Time Library Reference for the syntax of the scalar
new
operator and the vector
new []
operator. See also the C++ Language Reference for the
new
operator. With objects, optional construction parameters may be passed:
CMyObject o2 = new CMyObject();
CMyObject o2 = new CMyObject(p1, p2);
Other memory allocation functions are:
malloc
, see the Run-Time Library Reference about other CRT functions that call malloc. Some of them require freeing allocated memory (e.g. _getcwd
).calloc
LocalAlloc
GlobalAlloc
, code is often freed elsewhere.HeapAlloc
VirtualAlloc
- Various Windows SDK functions that allocate memory.