Skip to content
VecInitAlignedWithDeepCopyStack

VecInitAlignedWithDeepCopyStack

VecInitAlignedWithDeepCopyStack

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.

These vectors are best used where user doesn’t get a chance to or does not want to deinit vector, given that no data in vector needs to be deinitialized. Example includes, but does not limit to a Vec(i8), Vec(f32), etc…

Parameters

Name Direction Description
v in,out Vector that needs to be initialized.
ne in Number of elements to allocate aligned stack memory for.
ci in Copy init method for copying over elements in vector.
cd in Copy deinit method for deiniting elements in vector.
aln in Alignment value to align all emenets to.

Usage example (from documentation)

  Vec(Node*) nodes;

  // initialize vector with stack memory, aligned with 124 byte boundary
  VecInitAlignedWithDeepCopyStack(&nodes, 24, NodeInitCopy, NodeDeinit, 124, {
        // scope where vector memory is available
        FindAndFillAllNodes(&nodes, ... /* some other relevant data */);

        UseNodes(&nodes);

        VecForeach(&nodes, node, {
        });

        // vector deinit will be called for you after this automatically
        // so any data held by the vector in this scope is invalid outside
  });

Usage example (Cross-references)

Usage examples (Cross-references)
    /// TAGS: Init, Vec, Stack, Length, Size, Array
    ///
    #define VecInitStack(v, ne, scoped_body) VecInitAlignedWithDeepCopyStack(v, ne, NULL, NULL, 1, scoped_body)
    
    ///
    ///
    #define VecInitAlignedStack(v, ne, aln, scoped_body)                                                                   \
        VecInitAlignedWithDeepCopyStack(v, ne, NULL, NULL, aln, scoped_body)
    
    ///
    ///
    #define VecInitWithDeepCopyStack(v, ne, ci, cd, scoped_body)                                                           \
        VecInitAlignedWithDeepCopyStack(v, ne, ci, cd, 1, scoped_body)
    
    ///
Last updated on