@@ -223,6 +223,7 @@ $(GNAME AlignAttribute):
223
223
sets it to the default, which matches the default member alignment
224
224
of the companion C compiler.)
225
225
226
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
226
227
--------
227
228
struct S
228
229
{
@@ -232,10 +233,11 @@ struct S
232
233
long c; // placed at offset 8
233
234
}
234
235
static assert(S.alignof == 8);
236
+ static assert(S.c.offsetof == 8);
235
237
static assert(S.sizeof == 16);
236
238
--------
237
-
238
- $(P $(I AssignExpression) specifies the alignment
239
+ )
240
+ $(P The $(I AssignExpression) form specifies the alignment
239
241
which matches the behavior of the companion C compiler when non-default
240
242
alignments are used. It must be a positive power of 2.
241
243
)
@@ -244,6 +246,7 @@ static assert(S.sizeof == 16);
244
246
fields are packed together.
245
247
)
246
248
249
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
247
250
--------
248
251
struct S
249
252
{
@@ -253,13 +256,15 @@ struct S
253
256
long c; // placed at offset 5
254
257
}
255
258
static assert(S.alignof == 1);
259
+ static assert(S.c.offsetof == 5);
256
260
static assert(S.sizeof == 13);
257
261
--------
258
-
262
+ )
259
263
$(P The natural alignment of an aggregate is the maximum alignment of its
260
264
fields. It can be overridden by setting the alignment outside of the
261
265
aggregate.)
262
266
267
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
263
268
--------
264
269
align (2) struct S
265
270
{
@@ -269,12 +274,14 @@ align (2) struct S
269
274
long c; // placed at offset 5
270
275
}
271
276
static assert(S.alignof == 2);
277
+ static assert(S.c.offsetof == 5);
272
278
static assert(S.sizeof == 14);
273
279
--------
274
-
280
+ )
275
281
$(P Setting the alignment of a field aligns it to that power of 2, regardless
276
282
of the size of the field.)
277
283
284
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
278
285
--------
279
286
struct S
280
287
{
@@ -283,9 +290,19 @@ struct S
283
290
align (16) short c; // placed at offset 16
284
291
}
285
292
static assert(S.alignof == 16);
293
+ static assert(S.c.offsetof == 16);
286
294
static assert(S.sizeof == 32);
287
295
--------
296
+ )
297
+ $(P The $(I AlignAttribute) is reset to the default when
298
+ entering a function scope or a non-anonymous struct, union, class, and restored
299
+ when exiting that scope.
300
+ It is not inherited from a base class.
301
+ )
288
302
303
+ $(P See also: $(DDSUBLINK spec/struct, struct_layout, Struct Layout).)
304
+
305
+ $(H3 $(LNAME2 align_gc, GC Compatibility))
289
306
290
307
$(P Do not align references or pointers that were allocated
291
308
using $(GLINK2 expression, NewExpression) on boundaries that are not
@@ -294,15 +311,26 @@ static assert(S.sizeof == 32);
294
311
byte boundaries.
295
312
)
296
313
297
- $(UNDEFINED_BEHAVIOR If any pointers and references to GC
298
- allocated objects are not aligned on `size_t` byte boundaries.)
314
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
315
+ ---
316
+ struct S
317
+ {
318
+ align(1):
319
+ byte b;
320
+ int* p;
321
+ }
299
322
300
- $(P The $(I AlignAttribute) is reset to the default when
301
- entering a function scope or a non-anonymous struct, union, class, and restored
302
- when exiting that scope.
303
- It is not inherited from a base class.
304
- )
323
+ static assert(S.p.offsetof == 1);
305
324
325
+ @safe void main()
326
+ {
327
+ S s;
328
+ s.p = new int; // error: can't modify misaligned pointer in @safe code
329
+ }
330
+ ---
331
+ )
332
+ $(UNDEFINED_BEHAVIOR If any pointers and references to GC
333
+ allocated objects are not aligned on `size_t` byte boundaries.)
306
334
307
335
308
336
$(H2 $(LNAME2 deprecated, $(D deprecated) Attribute))
0 commit comments