File: deletearray.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (52 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (4)
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
49
50
51
52
Dynamically allocated arrays are deleted using operator
 ti(delete[]). It expects a pointer to a block of memory, previously allocated
by operator ti(new[]).

    When operator tt(delete[])'s operand is a pointer to an array of objects
two actions are performed:
    itemization(
    it() First, the class's destructor is called for each of the objects in
the array. The destructor, as explained link(later in this
chapter)(DESTRUCTOR), performs all kinds of cleanup operations that are
required by the time the object ceases to exist.
    it() Second, the memory pointed at by the pointer is returned to the
common pool.
    )
    Here is an example showing how to allocate and delete an array of 10
string objects:
        verb(    std::string *sp = new std::string[10];
    delete[] sp;)

No special action is performed if a dynamically allocated array of
primitive typed values is deleted. Following tt(int *it = new int[10]) the
statement tt(delete[] it) simply returns the memory pointed at by tt(it).
Realize that, as a pointer is a primitive type, deleting a dynamically
allocated array of pointers to objects does em(not) result in the proper
destruction of the objects the array's elements point at. So, the following
example results in a emi(memory leak):
        verb(    string **sp = new string *[5];
    for (size_t idx = 0; idx != 5; ++idx)
        sp[idx] = new string;
    delete[] sp;            // MEMORY LEAK !)

In this example the only action performed by tt(delete[]) is to return an
area the size of five pointers to strings to the common pool.

    Here's how the destruction in such cases em(should) be performed:
    itemization(
    it() Call tt(delete) for each of the array's elements;
    it() Delete the array itself
    )
    Example:
        verb(    for (size_t idx = 0; idx != 5; ++idx)
        delete sp[idx];
    delete[] sp;)

One of the consequences is of course that by the time the memory is going
to be returned not only the pointer must be available but also the number of
elements it contains. This can easily be accomplished by storing pointer and
number of elements in a simple class and then using an object of that class.

    Operator tt(delete[]) is a different operator than operator
tt(delete). The i(rule of thumb) is: if ti(new[]) was used, also use
tt(delete[]).