Skip to content

Commit d725dc1

Browse files
committed
arrays.dd: better wording
1 parent a816a21 commit d725dc1

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

spec/arrays.dd

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ int* p;
2424
---------
2525
)
2626

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.
2849
Pointers are provided for interfacing with C and for
2950
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
3252
compiler or runtime to do bounds checking, etc., on it.
3353
Most conventional uses for pointers can be replaced with
3454
dynamic arrays, $(D out) and $(D ref) parameters,
@@ -43,26 +63,30 @@ int[3] s;
4363
---------
4464
)
4565

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.
4867
)
4968

5069
$(P The total size of a static array cannot exceed 16Mb.
51-
A dynamic array should be used instead for such large arrays.
5270
)
5371

5472
$(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.
5874
)
5975

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.
6478
)
6579

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+
))
6690

6791
$(H3 $(LNAME2 dynamic-arrays, Dynamic Arrays))
6892

@@ -76,6 +100,13 @@ int[] a;
76100
Multiple dynamic arrays can share all or parts of the array data.
77101
)
78102

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+
79110
$(H2 $(LNAME2 declarations, Array Declarations))
80111

81112
$(P Declarations appear before the identifier being
@@ -98,8 +129,6 @@ $(H2 $(LNAME2 usage, Array Usage))
98129
affecting
99130
the handle to the array,
100131
and affecting the contents of the array.
101-
C only has
102-
operators to affect the handle. In D, both are accessible.
103132
)
104133

105134
$(P The handle to an array is specified by naming the array, as

0 commit comments

Comments
 (0)