@@ -112,6 +112,10 @@ a = s; // a is initialized to point to the s array
112
112
a = b; // a points to the same array as b does
113
113
---------
114
114
115
+ $(H2 $(LNAME2 indexing, Indexing))
116
+
117
+ $(P See also $(GLINK2 expression, IndexExpression).)
118
+
115
119
$(H2 $(LNAME2 slicing, Slicing))
116
120
117
121
$(P $(I Slicing) an array means to specify a subarray of it.
@@ -157,6 +161,9 @@ int* p;
157
161
int[] b = p[0..8];
158
162
---------
159
163
164
+ $(P See also $(GLINK2 expression, SliceExpression).)
165
+
166
+
160
167
$(H2 $(LNAME2 array-copying, Array Copying))
161
168
162
169
$(P When the slice operator appears as the left-hand side of an
@@ -576,17 +583,7 @@ assert(cap == array.capacity);
576
583
577
584
$(H3 $(LNAME2 func-as-property, Functions as Array Properties))
578
585
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)).)
590
587
591
588
$(H2 $(LNAME2 bounds, Array Bounds Checking))
592
589
@@ -635,6 +632,28 @@ int x = foo[3]; // error, out of bounds
635
632
with a compile time switch.
636
633
)
637
634
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
+
638
657
$(H2 $(LNAME2 array-initialization, Array Initialization))
639
658
640
659
$(H3 $(LNAME2 default-initialization, Default Initialization))
@@ -820,7 +839,7 @@ $(H4 $(LNAME2 printf, C's printf() and Strings))
820
839
821
840
---------
822
841
str ~= "\0";
823
- printf("the string is '%s'\n", cast(char*) str);
842
+ printf("the string is '%s'\n", str.ptr );
824
843
---------
825
844
826
845
or:
@@ -834,15 +853,15 @@ printf("the string is '%s'\n", std.string.toStringz(str));
834
853
can be used directly:)
835
854
836
855
-----------
837
- printf("the string is '%s'\n", cast(char*) "string literal");
856
+ printf("the string is '%s'\n", "string literal".ptr );
838
857
-----------
839
858
840
859
$(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)*` .
843
862
The rest of the arguments to printf, however, are variadic
844
863
(specified by ...),
845
- and a string literal is passed as a (length,pointer) combination
864
+ and a string literal typed `immutable(char)[]` cannot pass
846
865
to variadic parameters.)
847
866
848
867
$(P The second way is to use the precision specifier.
0 commit comments