Skip to content

Commit 4fd7517

Browse files
authored
Merge pull request #2342 from schlupa/patch-1
Fixed crashing example in slicing section merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents c31e5f5 + d803f2b commit 4fd7517

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

spec/arrays.dd

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ $(H2 $(LNAME2 slicing, Slicing))
165165

166166
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
167167
---------
168-
void foo(int);
168+
void foo(int value)
169+
{
170+
writeln("value=", value);
171+
}
169172

170173
int[10] a; // declare array of 10 ints
171174
int[] b;
@@ -182,14 +185,20 @@ foo(b[1]); // equivalent to foo(3)
182185
For example, the assignments to b:
183186
)
184187

185-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
188+
$(SPEC_RUNNABLE_EXAMPLE_RUN
186189
---------
187-
int[10] a;
188-
int[] b;
190+
int[10] a = [ 1,2,3,4,5,6,7,8,9,10 ];
191+
int[] b1, b2, b3, b4;
192+
193+
b1 = a;
194+
b2 = a[];
195+
b3 = a[0 .. a.length];
196+
b4 = a[0 .. $];
197+
writeln(b1);
198+
writeln(b2);
199+
writeln(b3);
200+
writeln(b4);
189201

190-
b = a;
191-
b = a[];
192-
b = a[0 .. a.length];
193202
---------
194203
)
195204

@@ -201,10 +210,17 @@ b = a[0 .. a.length];
201210
but for converting pointers into bounds-checked arrays:
202211
)
203212

204-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
213+
$(SPEC_RUNNABLE_EXAMPLE_RUN
205214
---------
206-
int* p;
215+
int[10] a = [ 1,2,3,4,5,6,7,8,9,10 ];
216+
217+
int* p = &a[2];
207218
int[] b = p[0..8];
219+
writeln(b);
220+
writeln(p[7]); // 10
221+
writeln(p[8]); // undefined behaviour
222+
writeln(b[7]); // 10
223+
//writeln(b[8]); // range error
208224
---------
209225
)
210226

0 commit comments

Comments
 (0)