VecInitAlignedWithDeepCopy

Table of Contents

VecInitAlignedWithDeepCopy

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
ciinCopy init method.
cdinCopy deinit method.
alninVector element alignment. All items will be stored by respecting the alignment boundary.

Usage example (from documentation)

  typedef Vec(Node) NodeVec;
  NodeVec nodes = VecInitAlignedWithDeepCopy(NodeInitCopy, NodeDeinit, 48);

Usage example (Cross-references)

    // Test vector initialization with alignment and deep copy functions
    bool test_vec_init_aligned_with_deep_copy(void) {
    WriteFmt("Testing VecInitAlignedWithDeepCopy\n");
    
    // Test with struct type, custom copy/deinit functions, and 8-byte alignment
    // Test with struct type, custom copy/deinit functions, and 8-byte alignment
    typedef Vec(TestItem) TestVec;
    TestVec vec = VecInitAlignedWithDeepCopy(TestItemCopyInit, TestItemDeinit, 8);
    
    // Check initial state
    /// TAGS: Init, Vec, Length, Size, Aligned
    ///
    #define VecInit() VecInitAlignedWithDeepCopy(NULL, NULL, 1)
    
    ///
    /// TAGS: Init, Vec, Length, Size, Aligned, DeepCopy, DeepDeinit
    ///
    #define VecInitWithDeepCopy(ci, cd) VecInitAlignedWithDeepCopy(ci, cd, 1)
    
    ///
    /// TAGS: Init, Vec, Length, Size, Aligned
    ///
    #define VecInitAligned(aln) VecInitAlignedWithDeepCopy(NULL, NULL, aln)
    
    ///
    
    #ifdef __cplusplus
    #    define VecInitAlignedWithDeepCopyT(v, ci, cd, aln) (TYPE_OF(v) VecInitAlignedWithDeepCopy((ci), (cd), (aln)))
    #else
    ///
    /// TAGS: Init, Vec, Length, Size, Aligned, DeepCopy, DeepDeinit
    ///
    #    define VecInitAlignedWithDeepCopyT(v, ci, cd, aln) ((TYPE_OF(v))VecInitAlignedWithDeepCopy((ci), (cd), (aln)))
    #endif

Share :

Related Posts

VecInitT

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

Read More

VecForeachInRangeIdx

VecForeachInRangeIdx Description Iterate over elements in a specific range of the given vector v at each index idx. The variables var and idx are declared and defined by this macro. idx will start from start and will go till end - 1

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