@@ -24,11 +24,31 @@ int* p;
24
24
---------
25
25
)
26
26
27
- $(P These are simple pointers to data, analogous to C pointers.
27
+ $(P A pointer to type $(I T) has a value which is a reference (address) to another
28
+ object of type $(I T). It is commonly called a $(I pointer to T).
29
+ )
30
+
31
+ $(P If a pointer contains a $(I null) value, it is not pointing to a valid object.)
32
+
33
+ $(P When a pointer to $(I T) is dereferenced, it must either contain a $(I null) value,
34
+ or point to a valid object of type $(I T).)
35
+
36
+ $(IMPLEMENTATION_DEFINED
37
+ $(OL
38
+ $(LI The behavior when a $(I null) pointer is dereferenced. Typically the program
39
+ will be aborted.)
40
+ ))
41
+
42
+ $(UNDEFINED_BEHAVIOR
43
+ $(OL
44
+ $(LI Dereferencing a pointer that is not $(I null) and does not point
45
+ to a valid object of type $(I T).)
46
+ ))
47
+
48
+ $(BEST_PRACTICE These are simple pointers to data.
28
49
Pointers are provided for interfacing with C and for
29
50
specialized systems work.
30
- There
31
- is no length associated with it, and so there is no way for the
51
+ There is no length associated with it, and so there is no way for the
32
52
compiler or runtime to do bounds checking, etc., on it.
33
53
Most conventional uses for pointers can be replaced with
34
54
dynamic arrays, $(D out) and $(D ref) parameters,
@@ -43,26 +63,30 @@ int[3] s;
43
63
---------
44
64
)
45
65
46
- $(P These are analogous to C arrays. Static arrays are distinguished
47
- by having a length fixed at compile time.
66
+ $(P Static arrays have a length fixed at compile time.
48
67
)
49
68
50
69
$(P The total size of a static array cannot exceed 16Mb.
51
- A dynamic array should be used instead for such large arrays.
52
70
)
53
71
54
72
$(P A static array with a dimension of 0 is allowed, but no
55
- space is allocated for it. It's useful as the last member
56
- of a variable length struct, or as the degenerate case of
57
- a template expansion.
73
+ space is allocated for it.
58
74
)
59
75
60
-
61
- $(P Static arrays are value types. Unlike in C and D version 1,
62
- static arrays are passed to functions by value.
63
- Static arrays can also be returned by functions.
76
+ $(P Static arrays are value types.
77
+ They are passed to and returned by functions by value.
64
78
)
65
79
80
+ $(BEST_PRACTICE
81
+ $(OL
82
+ $(LI Use dynamic arrays for larger arrays.)
83
+ $(LI Static arrays with 0 elements are useful as the last member
84
+ of a variable length struct, or as the degenerate case of
85
+ a template expansion.)
86
+ $(LI Because static arrays are passed to functions by value,
87
+ a larger array can consume a lot of stack space. Use dynamic arrays
88
+ instead.)
89
+ ))
66
90
67
91
$(H3 $(LNAME2 dynamic-arrays, Dynamic Arrays))
68
92
@@ -76,6 +100,13 @@ int[] a;
76
100
Multiple dynamic arrays can share all or parts of the array data.
77
101
)
78
102
103
+ $(BEST_PRACTICE
104
+ $(OL
105
+ $(LI Use dynamic arrays instead of pointers to arrays as much as practical.
106
+ Indexing of dynamic arrays are bounds checked, avoiding buffer underflow and
107
+ overflow problems.)
108
+ ))
109
+
79
110
$(H2 $(LNAME2 declarations, Array Declarations))
80
111
81
112
$(P Declarations appear before the identifier being
@@ -98,8 +129,6 @@ $(H2 $(LNAME2 usage, Array Usage))
98
129
affecting
99
130
the handle to the array,
100
131
and affecting the contents of the array.
101
- C only has
102
- operators to affect the handle. In D, both are accessible.
103
132
)
104
133
105
134
$(P The handle to an array is specified by naming the array, as
0 commit comments