Skip to content

Commit b369fee

Browse files
authored
[spec/arrays] Improve Initialization section and add String Literals (#4309)
Add links. Clarify the void initialization only affects static array elements. Add panel for enum indices to array initializer example. Add *String Literal Initializers* subsection.
1 parent d99da9b commit b369fee

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

spec/arrays.dd

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,8 @@ $(H3 $(LNAME2 default-initialization, Default Initialization))
10651065

10661066
$(UL
10671067
$(LI Pointers are initialized to $(D null).)
1068-
$(LI Static array contents are initialized to the default
1069-
initializer for the array element type.)
1068+
$(LI Static array contents are initialized to the
1069+
$(DDSUBLINK spec/property, init, default initializer) for the array element type.)
10701070
$(LI Dynamic arrays are initialized to having 0 elements and a null `.ptr`.)
10711071
$(LI Associative arrays are initialized to having 0 elements.)
10721072
)
@@ -1093,10 +1093,16 @@ assert(j[0].length == 5);
10931093

10941094
$(H3 $(LNAME2 void-initialization, Void Initialization))
10951095

1096-
$(P Void initialization happens when the $(I Initializer) for
1097-
an array is $(D void). What it means is that no initialization
1098-
is done, i.e. the contents of the array will be undefined.
1096+
$(P Void initialization happens when the $(GLINK2 declaration, Initializer) for
1097+
an array declaration is $(D void). For a static array,
1098+
it means no initialization
1099+
is done - i.e. the contents of the array will be undefined.
10991100
This is most useful as an efficiency optimization.
1101+
)
1102+
---
1103+
int[256] arr = void; // elements of `arr` have undefined bit-patterns
1104+
---
1105+
$(BEST_PRACTICE
11001106
Void initializations are an advanced technique and should only be used
11011107
when profiling indicates that it matters.
11021108
)
@@ -1137,7 +1143,7 @@ assert(a == [0, 2, 3]);
11371143
---------
11381144
)
11391145

1140-
$(P This is most handy when the array indices are given by $(DDLINK spec/enum, Enums, enums):)
1146+
$(PANEL This is most handy when the array indices are given by $(DDLINK spec/enum, Enums, enums):
11411147

11421148
$(SPEC_RUNNABLE_EXAMPLE_RUN
11431149
---------
@@ -1151,6 +1157,7 @@ int[Color.max + 1] value =
11511157
assert(value == [5, 6, 2]);
11521158
---------
11531159
)
1160+
)
11541161

11551162
$(P Any indices must be known at compile-time.
11561163
Note that if the array type is not specified and every element has an index,
@@ -1186,6 +1193,29 @@ assert(a == [42, 42, 42, 42]);
11861193
Otherwise, they need to be marked with $(D const) or $(D static)
11871194
storage classes to make them statically allocated arrays.)
11881195

1196+
$(H4 $(LNAME2 static-string, String Literal Initializers))
1197+
1198+
$(P A static array of character type is $(RELATIVE_LINK2 default-initialization,
1199+
default-initialized) the same as other
1200+
static arrays. However, when initialized from a string literal:)
1201+
1202+
* The literal can be of a shorter length than the array.
1203+
* Any remaining elements are initialized to `\0`.
1204+
1205+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1206+
---
1207+
char[10] s1;
1208+
assert(s1[9] == '\xFF'); // char.init
1209+
1210+
char[10] s2 = "hi";
1211+
assert(s2[9] == '\0');
1212+
---
1213+
)
1214+
$(RATIONALE A mutable character array's `.ptr` property can be used to
1215+
obtain a C string (if it has an element which is zero). When that array
1216+
is initialized with a shorter string literal, it's likely the subsequent
1217+
code will need zero-termination.)
1218+
11891219

11901220
$(H2 $(LNAME2 special-array, Special Array Types))
11911221

0 commit comments

Comments
 (0)