VecInitStack

Table of Contents

VecInitStack

Description

Initialize given vector using memory from stack. Such vectors cannot be dynamically resized. Doing so is UB. It is mandatory to initialize vectors before use. Not doing so is undefined behaviour. 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… Stack inited vectors mustn’t be deinited after use.

Parameters

NameDirectionDescription
vin,outVector that needs to be initialized.
neinNumber of elements to allocate stack memory for.

Usage example (from documentation)

  Vec(i32) ids;
  VecInitStack(ids, 64, {
      // scope where vector memory is available
      ids = MakeClientRequestToFillVector(ids);
      VecForeach(&ids, id, {
          // some relevant logic
      });

      // Do not call deinit after use!!
  });

Usage example (Cross-references)

    /// FAILURE : NULL
    ///
    #define StrInitStack(str, ne, scoped_body) VecInitStack(str, ne, scoped_body)
    
    ///
    // Test vector stack initialization
    bool test_vec_init_stack(void) {
    printf("Testing VecInitStack\n");
    
    bool result = true;
    IntVec vec;
    
    VecInitStack(vec, 10, {
    // Inside the scope where the stack vector is valid
    TestVec test_vec;
    
    VecInitStack(test_vec, 5, {
    // Inside the scope where the stack vector is valid

Share :

Related Posts

VecInitAlignedWithDeepCopy

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.

Read More

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

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.

Read More