Skip to content
VecInitAlignedWithDeepCopyT

VecInitAlignedWithDeepCopyT

VecInitAlignedWithDeepCopyT

Description

Initialize given 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
v in Variable or type of a vector to be initialized.
ci in Copy init method.
cd in Copy deinit method.
aln in Vector element alignment. All items will be stored by respecting the alignment boundary.

Usage example (from documentation)

    bool DataInitClone(Data* dst, Data* src) { /* cloning logic...*/ }
    void DataDeinit(Data* d) { /* deinit logic... don't free "d" */ }

    void SomeInterestingFn(DataVec data_vec) {
        // align and store all items in vector at 32 byte boundaries
        data_vec = VecInitAlignedWithDeepCopyT(data_vec, DataInitClone, DataDeinit, 32);

        // use vector
        // always use VecAt to access elements in vector
        Data i1 = VecAt(data_vec, 9); // get 10th item (index = 9)
        Data i2 = VecLast(data_vec); // get last item (index = whatever)
    }

Usage example (Cross-references)

Usage examples (Cross-references)
    /// TAGS: Init, Vec, Length, Size, Aligned
    ///
    #define VecInitT(v) VecInitAlignedWithDeepCopyT(v, NULL, NULL, 1)
    
    ///
    /// TAGS: Init, Vec, Length, Size, Aligned, DeepCopy, DeepDeinit
    ///
    #define VecInitWithDeepCopyT(v, ci, cd) VecInitAlignedWithDeepCopyT(v, ci, cd, 1)
    
    ///
    /// TAGS: Init, Vec, Length, Size, Aligned
    ///
    #define VecInitAlignedT(v, aln) VecInitAlignedWithDeepCopyT(v, NULL, NULL, aln)
    
    ///
    
    #ifdef __cplusplus
    #    define VecInitAlignedWithDeepCopyT(v, ci, cd, aln) (TYPE_OF(v) VecInitAlignedWithDeepCopy((ci), (cd), (aln)))
    #else
    ///
            char ___data___[ALIGN_UP(sizeof(VEC_DATATYPE(&(v))), (aln)) * ((ne) + 1)] = {0};                               \
                                                                                                                           \
            (v)          = VecInitAlignedWithDeepCopyT((v), (ci), (cd), (aln));                                            \
            (v).capacity = (ne);                                                                                           \
            (v).data     = (VEC_DATATYPE(&(v)) *)&___data___[0];                                                           \
Last updated on