File: newarray.yo

package info (click to toggle)
c%2B%2B-annotations 8.2.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,804 kB
  • ctags: 2,845
  • sloc: cpp: 15,418; makefile: 2,473; ansic: 165; perl: 90; sh: 29
file content (59 lines) | stat: -rw-r--r-- 3,345 bytes parent folder | download | duplicates (3)
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
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, tt(struct)
types without constructors) the block of memory returned by operator tt(new[])
is em(not) guaranteed to be initialized to 0.

    When operator tt(new[]) is used to allocate arrays of objects their
constructors are automatically used. Consequently tt(new string[20]) results
in a block of 20 em(initialized) tt(string) objects. When allocating arrays of
objects the class's
  emi(default constructor) is used to initialize each individual object in
turn. 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.