VecInitAligned
- Macro
- August 22, 2025
Table of Contents
VecInitAligned
VecInitAligned
Description
Initialize vector with given alignment. It is mandatory to initialize vectors before use. Not doing so is undefined behaviour. Provided alignment is used to keep all objects at an aligned memory location, avoiding UB in some cases. It’s recommended to use aligned vector when dealing with structs containing unions.
Parameters
Name | Direction | Description |
---|---|---|
aln | in | Vector element alignment. All items will be stored by respecting the alignment boundary. |
Usage example (from documentation)
Vec(Node) nodes = VecInitAligned(16);
Usage example (Cross-references)
- In
Vec.Access.c:178
:
// Test with a vector with alignment > 1
typedef Vec(int) AlignedIntVec;
AlignedIntVec aligned_vec = VecInitAligned(8);
// Add some data
- In
Vec.Access.c:215
:
// Create a vector with 8-byte alignment
typedef Vec(int) AlignedIntVec;
AlignedIntVec aligned_vec = VecInitAligned(8);
// For 8-byte alignment, each int (4 bytes) should be padded to 8 bytes
- In
Vec.Init.c:73
:
// Test aligned vector initialization
bool test_vec_init_aligned(void) {
printf("Testing VecInitAligned\n");
// Test with int type and 4-byte alignment
- In
Vec.Init.c:77
:
// Test with int type and 4-byte alignment
typedef Vec(int) IntVec;
IntVec vec = VecInitAligned(4);
// Check initial state
- In
Vec.Init.c:89
:
// Test with struct type and 16-byte alignment
typedef Vec(TestItem) TestVec;
TestVec test_vec = VecInitAligned(16);
// Check initial state