Skip to content

Commit e9d2818

Browse files
authored
Merge pull request #2002 from wilzbach/1040-arrays
Revive #1040 - spec/arrays.d merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
2 parents 5f77e76 + b4d058d commit e9d2818

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

spec/arrays.dd

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ a = s; // a is initialized to point to the s array
112112
a = b; // a points to the same array as b does
113113
---------
114114

115+
$(H2 $(LNAME2 indexing, Indexing))
116+
117+
$(P See also $(GLINK2 expression, IndexExpression).)
118+
115119
$(H2 $(LNAME2 slicing, Slicing))
116120

117121
$(P $(I Slicing) an array means to specify a subarray of it.
@@ -157,6 +161,9 @@ int* p;
157161
int[] b = p[0..8];
158162
---------
159163

164+
$(P See also $(GLINK2 expression, SliceExpression).)
165+
166+
160167
$(H2 $(LNAME2 array-copying, Array Copying))
161168

162169
$(P When the slice operator appears as the left-hand side of an
@@ -576,17 +583,7 @@ assert(cap == array.capacity);
576583

577584
$(H3 $(LNAME2 func-as-property, Functions as Array Properties))
578585

579-
$(P If the first parameter to a function is an array, the
580-
function can be called as if it were a property of the array:
581-
)
582-
583-
---
584-
int[] array;
585-
void foo(int[] a, int x);
586-
587-
foo(array, 3);
588-
array.foo(3); // means the same thing
589-
---
586+
$(P See $(DDSUBLINK function, pseudo-member, Uniform Function Call Syntax (UFCS)).)
590587

591588
$(H2 $(LNAME2 bounds, Array Bounds Checking))
592589

@@ -635,6 +632,28 @@ int x = foo[3]; // error, out of bounds
635632
with a compile time switch.
636633
)
637634

635+
$(P An out of bounds memory access will cause undefined behavior,
636+
therefore array bounds check is normally enabled in `@safe` functions.
637+
The runtime behavior is part of the language semantics.
638+
)
639+
640+
$(P See also $(DDSUBLINK function, safe-functions, Safe Functions).)
641+
642+
$(H3 $(LNAME2 disable-bounds-check, Disabling Array Bounds Checking))
643+
644+
$(P Insertion of array bounds checking code at runtime may be
645+
turned off with a compiler switch $(LINK2 $(ROOT_DIR)dmd.html#switch-boundscheck, `-boundscheck`).
646+
)
647+
648+
$(P If the bounds check in `@system` or `@trusted` code is disabled,
649+
the code correctness must still be guaranteed by the code author.
650+
)
651+
652+
$(P On the other hand, disabling the bounds check in `@safe` code will
653+
break the guaranteed memory safety by compiler. It's not recommended
654+
unless motivated by speed measurements.
655+
)
656+
638657
$(H2 $(LNAME2 array-initialization, Array Initialization))
639658

640659
$(H3 $(LNAME2 default-initialization, Default Initialization))
@@ -820,7 +839,7 @@ $(H4 $(LNAME2 printf, C's printf() and Strings))
820839

821840
---------
822841
str ~= "\0";
823-
printf("the string is '%s'\n", cast(char*)str);
842+
printf("the string is '%s'\n", str.ptr);
824843
---------
825844

826845
or:
@@ -834,15 +853,15 @@ printf("the string is '%s'\n", std.string.toStringz(str));
834853
can be used directly:)
835854

836855
-----------
837-
printf("the string is '%s'\n", cast(char*)"string literal");
856+
printf("the string is '%s'\n", "string literal".ptr);
838857
-----------
839858

840859
$(P So, why does the first string literal to printf not need
841-
the cast? The first parameter is prototyped as a const(char)*, and
842-
a string literal can be implicitly cast to a const(char)*.
860+
the `.ptr`? The first parameter is prototyped as a `const(char)*`, and
861+
a string literal can be implicitly `cast` to a `const(char)*`.
843862
The rest of the arguments to printf, however, are variadic
844863
(specified by ...),
845-
and a string literal is passed as a (length,pointer) combination
864+
and a string literal typed `immutable(char)[]` cannot pass
846865
to variadic parameters.)
847866

848867
$(P The second way is to use the precision specifier.

0 commit comments

Comments
 (0)