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