-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathNewDeleteArrayMismatch.qhelp
More file actions
38 lines (29 loc) · 1.16 KB
/
NewDeleteArrayMismatch.qhelp
File metadata and controls
38 lines (29 loc) · 1.16 KB
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
This rule finds <code>delete[]</code> expressions that are using a pointer that points to memory
allocated using the <code>new</code> operator. Behavior in such cases is undefined and should
be avoided.
</p>
<p>
The <code>new</code> operator allocates memory for just <em>one</em> object, then calls that object's constructor, and <code>delete</code>
does the opposite. The array <code>delete[]</code> operator, however, expects the pointer to be pointing to the first element of
an array (which could have header data specifying the length of the array) and would attempt to call the destructor on each
element of the 'array', which would likely lead to a segfault due to the invalid header data.
</p>
<include src="pointsToWarning.inc.qhelp" />
</overview>
<recommendation>
<p>
Use the <code>delete</code> operator when freeing memory allocated with <code>new</code>.
</p>
</recommendation>
<example><sample src="NewDeleteArrayMismatch.cpp" />
</example>
<references>
<li>S. Meyers. <em>Effective C++ 3d ed.</em> pp 73-75. Addison-Wesley Professional, 2005.</li>
</references>
</qhelp>