@@ -18,13 +18,24 @@ $(H2 $(LNAME2 array-kinds, Kinds))
18
18
19
19
$(H3 $(LNAME2 pointers, Pointers))
20
20
21
+ $(P A pointer to type $(D T) has a value which is a reference (address) to another
22
+ object of type $(D T). It is commonly called a $(I pointer to T) and its type is
23
+ `T*`. To access the object value, use the `*` dereference operator:
24
+ )
25
+
26
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
21
27
---------
22
28
int* p;
23
- ---------
24
29
25
- $(P A pointer to type $(I T) has a value which is a reference (address) to another
26
- object of type $(I T). It is commonly called a $(I pointer to T).
27
- )
30
+ assert(p == null);
31
+ p = new int(5);
32
+ assert(p != null);
33
+
34
+ assert(*p == 5);
35
+ (*p)++;
36
+ assert(*p == 6);
37
+ ---------
38
+ )
28
39
29
40
$(P If a pointer contains a $(I null) value, it is not pointing to a valid object.)
30
41
@@ -43,13 +54,34 @@ int* p;
43
54
to a valid object of type $(I T).)
44
55
))
45
56
46
- $(BEST_PRACTICE These are simple pointers to data.
47
- Pointers are provided for interfacing with C and for
57
+ $(P To set a pointer to point at an existing object, use the
58
+ `&` *address of* operator:)
59
+
60
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
61
+ ---------
62
+ int i = 2;
63
+ int* p = &i;
64
+
65
+ assert(p == &i);
66
+ assert(*p == 2);
67
+ *p = 4;
68
+ assert(i == 4);
69
+ ---------
70
+ )
71
+
72
+ $(P These are simple pointers to data.
73
+ A pointer can manipulate a block of multiple values. Accessing more
74
+ than one value cannot be
75
+ $(DDLINK spec/memory-safe-d, Memory-Safe-D-Spec, `@safe`) as it
76
+ requires $(RELATIVE_LINK2 pointer-arithmetic, pointer arithmetic).
77
+ This is supported for interfacing with C and for
48
78
specialized systems work.
49
- There is no length associated with it, and so there is no way for the
79
+ A pointer has no length associated with it, so there is no way for the
50
80
compiler or runtime to do bounds checking, etc., on it.
51
- Most conventional uses for pointers can be replaced with
52
- dynamic arrays, $(D out) and $(D ref) parameters,
81
+ )
82
+
83
+ $(BEST_PRACTICE Most conventional uses for pointers can be replaced with
84
+ dynamic arrays, $(D ref) and $(D out) $(DDSUBLINK function, parameters, parameters),
53
85
and reference types.
54
86
)
55
87
@@ -192,6 +224,29 @@ assert(b[1] == 2);
192
224
193
225
$(P See also $(GLINK2 expression, IndexExpression).)
194
226
227
+ $(H3 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
228
+
229
+ $(P A pointer can also be indexed, but no bounds checks are done.
230
+ Unlike arrays, a pointer value can also be used in certain
231
+ arithmetic expressions to produce another pointer:)
232
+
233
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
234
+ ---
235
+ int[] a = [1,2,3];
236
+ int* p = a.ptr;
237
+
238
+ p[2] = 4;
239
+ assert(a[2] == 4);
240
+ writeln(p[3]); // undefined behaviour
241
+
242
+ assert(p == &a[0]);
243
+ p++; // point to a[1]
244
+ assert(*p == 2);
245
+ ---
246
+ )
247
+
248
+ $(P See $(DDSUBLINK spec/expression, pointer_arithmetic, *AddExpression*) for details.)
249
+
195
250
$(H2 $(LNAME2 slicing, Slicing))
196
251
197
252
$(P $(I Slicing) an array means to specify a subarray of it.
@@ -231,12 +286,14 @@ $(SPEC_RUNNABLE_EXAMPLE_RUN
231
286
int[10] a = [ 1,2,3,4,5,6,7,8,9,10 ];
232
287
233
288
int* p = &a[2];
234
- int[] b = p[0..8];
235
- writeln(b);
236
289
writeln(p[7]); // 10
237
290
writeln(p[8]); // undefined behaviour
291
+
292
+ int[] b = p[0..8]; // convert pointer elements to dynamic array
293
+ assert(b is a[2..10]);
294
+ writeln(b);
238
295
writeln(b[7]); // 10
239
- //writeln(b[8]); // range error
296
+ //writeln(b[8]); // runtime error (unless bounds checks turned off)
240
297
---------
241
298
)
242
299
@@ -452,30 +509,6 @@ a[] -= (b[] + 4) * c[];
452
509
the target computer.
453
510
)
454
511
455
- $(H2 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
456
-
457
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
458
- ---------
459
- void dibb(int* array)
460
- {
461
- array[2]; // means same thing as *(array + 2)
462
- *(array + 2); // get 3rd element
463
- }
464
-
465
- void diss(int[] array)
466
- {
467
- array[2]; // ok
468
- *(array + 2); // error, array is not a pointer
469
- }
470
-
471
- void ditt(int[3] array)
472
- {
473
- array[2]; // ok
474
- *(array + 2); // error, array is not a pointer
475
- }
476
- ---------
477
- )
478
-
479
512
$(H2 $(LNAME2 rectangular-arrays, Rectangular Arrays))
480
513
481
514
$(P Experienced FORTRAN numerics programmers know that multidimensional
0 commit comments