VecInitAligned

Table of Contents

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

NameDirectionDescription
alninVector 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)

    // Test with a vector with alignment > 1
    typedef Vec(int) AlignedIntVec;
    AlignedIntVec aligned_vec = VecInitAligned(8);
    
    // Add some data
    // 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
    // Test aligned vector initialization
    bool test_vec_init_aligned(void) {
    WriteFmt("Testing VecInitAligned\n");
    
    // Test with int type and 4-byte alignment
    // Test with int type and 4-byte alignment
    typedef Vec(int) IntVec;
    IntVec vec = VecInitAligned(4);
    
    // Check initial state
    // Test with struct type and 16-byte alignment
    typedef Vec(TestItem) TestVec;
    TestVec test_vec = VecInitAligned(16);
    
    // Check initial state

Share :

Related Posts

VecForeachPtrReverse

VecForeachPtrReverse Description Iterate over each element var (as a pointer) of the given vector v in reverse order. This is a convenience macro that iterates backward using an internally managed index and provides a pointer to each element. The variable var is declared and defined by this macro as a pointer to the vector’s data type.

Read More

VecInit

VecInit Description Initialize vector. Default alignment is 1 It is mandatory to initialize vectors before use. Not doing so is undefined behaviour.

Read More

VecForeachInRange

VecForeachInRange Description Iterate over elements in a specific range of the given vector v. This is a convenience macro that iterates over a range using an internally managed index. The variable var is declared and defined by this macro.

Read More