File: newarray.yo

package info (click to toggle)
c%2B%2B-annotations 10.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,536 kB
  • ctags: 3,247
  • sloc: cpp: 19,157; makefile: 1,521; ansic: 165; sh: 128; perl: 90
file content (92 lines) | stat: -rw-r--r-- 4,425 bytes parent folder | download | duplicates (2)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Operator ti(new[]) is used to i(allocate arrays). The generic notation
tt(new[]) is used in the annotations(). Actually, the number of elements to be
allocated must be specified between the square brackets and it must, in turn,
be em(prefixed) by the type of the entities that must be allocated. Example:
        verb(
    int *intarr = new int[20];          // allocates 20 ints
    string *stringarr = new string[10]; // allocates 10 strings.
        )
    Operator tt(new) is a different operator than operator tt(new[]). A
consequence of this difference is discussed in the next section
(ref(DELETEARRAY)).

    Arrays allocated by operator tt(new[]) are called
 hi(array: dynamic)emi(dynamic arrays).  They are constructed during the
execution of a program, and their lifetime may exceed the lifetime of the
function in which they were created. Dynamically allocated arrays may last for
as long as the program runs.

    When tt(new[]) is used to allocate an array of primitive values or an
array of objects, tt(new[]) must be specified with a type and an (unsigned)
expression between its square brackets. The type and expression together are
used by the compiler to determine the required size of the block of memory to
make available. When tt(new[]) is used the array's elements are stored
consecutively in memory. An array index expression may thereafter be used to
access the array's individual elements: tt(intarr[0]) represents the first
tt(int) value, immediately followed by tt(intarr[1]), and so on until the last
element (tt(intarr[19])).

 hi(memory: initialization)With non-class types (primitive types, POD
types without constructors) the block of memory returned by operator tt(new[])
is em(not) guaranteed to be initialized to 0. Alternatively, adding tt(()) to
the tt(new) expression em(will) initialize the block of memory to
zeroes. E.g.,
        verb(
    struct POD
    {
        int iVal;
        double dVal;
    };
    new POD[5]();       // returns a pointer to 5 0-initialized PODs
    new double[9]();    // returns a pointer to 9 0-initialized doubles
        )
    If there are members of the tt(struct POD) that are explicitly initialized
in the struct's interface (e.g., tt(int iVal = 12)), or if the struct uses
composition, and the composed data member's type defines a default
constructor, then initializations in the struct's interface and
initializations performed by the composed data member's constructor takes
precedence over the 0-initialization. Here is an example:
        verb(
    struct Data
    {
        int value = 100;
    };
    struct POD
    {
        int iVal = 12;
        double dVal;
        Data data;
    };

    POD *pp = new POD[5]();
        )
    Here, tt(pp) points to five tt(POD) objects, each having their tt(iVal)
data members initialized to 12, their tt(dVal) data members initialized to 0,
and their tt(data.value) members initialized to 100.

    When operator tt(new[]) is used to allocate arrays of objects of class
types defining default constructors these constructors are automatically
used. Consequently tt(new string[20]) results in a block of 20 em(initialized)
tt(string) objects. A non-default constructor cannot be called, but often it
is possible to work around that (as discussed in section ref(NONDEFINIT)).

    The expression between brackets of operator tt(new[]) represents the
number of elements of the array to allocate. The bf(C++) standard allows
allocation of hi(array: 0-sized) 0-sized arrays. The statement
 hi(new Type[0])tt(new int[0]) is correct bf(C++). However, it is also
pointless and confusing and should be avoided. It is pointless as it doesn't
refer to any element at all, it is confusing as the returned pointer has a
useless non-0 value. A pointer intending to point to an array of values should
be initialized (like any pointer that isn't yet pointing to memory) to 0,
allowing for expressions like tt(if (ptr) ...)

    Without using operator tt(new[]), arrays of variable sizes can also be
constructed as
 hi(array: local)emi(local arrays). Such arrays are not dynamic arrays and
their lifetimes are restricted to the lifetime of the block in which they were
defined.

Once allocated, all arrays hi(array: fixed size) have fixed sizes. There is no
em(simple) way to enlarge or shrink arrays. bf(C++) has no operator
`ti(renew)'. Section ref(ENLARGEARRAY) illustrates how to hi(array: enlarge)
enlarge arrays.