Skip to content

Commit 48f6b69

Browse files
authored
[spec/type] Improve Pointer docs (#4281)
* [spec/type] Tweak pointer docs A pointer value is a memory address. Spec should not use 'object' except for struct/class instances. Aborting on null dereference is required in safe code (with link). * Use has/is null rather than 'contains' for clarity * Add a link to 'lvalue' * Add links to slicing and conversions * Prefer using `ref` to pointer
1 parent d8e6268 commit 48f6b69

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

spec/type.dd

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ AliasSeq!(int, string) tsi; // type sequence instance
165165

166166
$(H3 $(LNAME2 pointers, Pointers))
167167

168-
$(P A pointer to type $(D T) has a value which is a reference (address) to another
169-
object of type $(D T). It is commonly called a $(I pointer to T) and its type is
170-
`T*`. To access the object value, use the `*` dereference operator:
168+
$(P A pointer value is a memory address.
169+
A pointer to type $(D T) has a value which is a reference to an
170+
instance of type $(D T). It is commonly called a $(I pointer to T) and its type is
171+
`T*`. To access the pointed-to value, use the `*` dereference operator:
171172
)
172173

173174
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -184,21 +185,22 @@ assert(*p == 6);
184185
---------
185186
)
186187

187-
$(P If a pointer contains a $(I null) value, it is not pointing to a valid object.)
188+
$(P If a pointer has a $(I null) value, it is not pointing to valid data.)
188189

189-
$(P When a pointer to $(I T) is dereferenced, it must either contain a $(I null) value,
190-
or point to a valid object of type $(I T).)
190+
$(P When a pointer to $(I T) is dereferenced, it must either have a $(I null) value,
191+
or point to a valid instance of type $(I T).)
191192

192193
$(IMPLEMENTATION_DEFINED
193194
$(OL
194195
$(LI The behavior when a $(I null) pointer is dereferenced. Typically the program
195-
will be aborted.)
196+
will be aborted - $(DDSUBLINK spec/function, null-dereferences, that is required)
197+
in `@safe` code.)
196198
))
197199

198200
$(UNDEFINED_BEHAVIOR dereferencing a pointer that is not $(I null) and does not point
199-
to a valid object of type $(I T).)
201+
to a valid instance of type $(I T).)
200202

201-
$(P To set a pointer to point at an existing object, use the
203+
$(P To set a pointer to point at an existing $(DDSUBLINK spec/expression, .define-lvalue, lvalue), use the
202204
`&` *address of* operator:)
203205

204206
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -212,7 +214,14 @@ assert(*p == 2);
212214
assert(i == 4);
213215
---------
214216
)
215-
$(P See also $(DDSUBLINK spec/expression, pointer_arithmetic, Pointer Arithmetic).)
217+
$(P See also:)
218+
* $(DDSUBLINK spec/expression, pointer_arithmetic, Pointer Arithmetic)
219+
* $(DDSUBLINK spec/expression, slice_expressions, Pointer Slicing)
220+
* $(RELATIVE_LINK2 pointer-conversions, Pointer Conversions)
221+
222+
$(BEST_PRACTICE Use a $(DDSUBLINK spec/function, ref-params,
223+
`ref` function parameter) or a $(DDSUBLINK spec/declaration, ref-variables,
224+
`ref` local variable) when the address doesn't escape.
216225

217226

218227
$(H2 $(LEGACY_LNAME2 User Defined Types, user-defined-types, User-Defined Types))

0 commit comments

Comments
 (0)