Alignment is determined by the type of the object and directives associated with the object itself. It isn't determined by the value from which it is initialized.
In other words, there's nothing you can place on the right of uint32_t foo[] =
that will affect how the array or individual elements of the array are aligned.
Let's compare.
In the linked post
char *u = ALIGNED_STRING("agsdas");
This produces two objects.
u <anon>
alignment = char* alignment = 16
+----------------+ +---+---+---+-...-+
| -------------->| a | g | s | ... |
+----------------+ +---+---+---+-...-+
As you can see, ALIGNED_STRING
has no effect on the alignment of the variable (u
), just the alignment of the anon object to which u
will point.
In your post
uint32_t foo[] = ...;
This produces a single object.
foo
alignment = uint32_t[] = uint32_t
+----------------+
| |
+----------------+
| |
+----------------+
| |
⋮ ⋮
| |
+----------------+
If you had an array of pointers to uint32_t
, you could align those uint32_t
as you wish.
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main( void ){
_Alignas( 16 ) uint32_t i1 = 1;
_Alignas( 16 ) uint32_t i2 = 2;
uint32_t i3 = 3;
uint32_t i4 = 4;
uint32_t *ptrs[] = {
&i1,
&i2,
&i3,
&i4,
};
size_t n = sizeof(ptrs)/sizeof(*ptrs);
for ( size_t i=0; i<n; ++i ) {
uint32_t *ptr = ptrs[i];
printf( "%p %" PRIu32 "\n", (void *)ptr, *ptr );
}
}
We can even make those object anonymous. Anonymous objects can be created using the ( type ){ initializer body }
syntax, and can use _Alignas
as part of the type to align the object.
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#define ALIGNED_UINT32( S, I ) ( ( _Alignas( S ) uint32_t ){ I } )
#define ANON_UINT32( I ) ( ( uint32_t ){ I } )
int main( void ){
uint32_t *ptrs[] = {
&ALIGNED_UINT32( 16, 1 ),
&ALIGNED_UINT32( 16, 2 ),
&ANON_UINT32( 3 ),
&ANON_UINT32( 4 ),
};
size_t n = sizeof(ptrs)/sizeof(*ptrs);
for ( size_t i=0; i<n; ++i ) {
uint32_t *ptr = ptrs[i];
printf( "%p %" PRIu32 "\n", (void *)ptr, *ptr );
}
}
Both of the above produce five objects.
ptrs
alignment = uint32_t*
+----------------+ +---+---+---+---+ i1/<anon>
| -------------------------->| 1 | alignment = 16
+----------------+ +---+---+---+---+
| ----------------------+
+----------------+ | +---+---+---+---+ i2/<anon>
| -----------------+ +--->| 2 | alignment = 16
+----------------+ | +---+---+---+---+
| ------------+ |
+----------------+ | | +---+---+---+---+ i3/<anon>
| +-------->| 3 | alignment = uint32_t
| +---+---+---+---+
|
| +---+---+---+---+ i4/<anon>
+------------->| 4 | alignment = uint32_t
+---+---+---+---+
Sample run:
0x7ffe29b31b30 1
0x7ffe29b31b20 2
0x7ffe29b31b1c 3
0x7ffe29b31b18 4
Demo on Compiler Explorer