VecForeachPtrInRangeIdx

Table of Contents

VecForeachPtrInRangeIdx

Description

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

Parameters

NameDirectionDescription
vin,outVector to iterate over.
varinName of pointer variable to be used which’ll point to value at iterated index idx.
idxinName of variable to be used for iterating over indices.
startinStarting index (inclusive).
endinEnding index (exclusive).

Usage example (Cross-references)

    if (start < end) {
    size_t total_len = 0;
    VecForeachPtrInRangeIdx(vec, str_ptr, idx, start, end) {
    total_len += strlen(*str_ptr) + idx;
    }
    if (start < end) {
    size_t total_len = 0;
    VecForeachPtrInRangeIdx(vec, str_ptr, idx, start, end) {
    total_len += ZstrLen(str_ptr->data) + idx;
    }
    if (start < end) {
    int sum = 0;
    VecForeachPtrInRangeIdx(vec, item_ptr, idx, start, end) {
    sum += *item_ptr + (int)idx;
    }
    // Make idx go out of bounds in VecForeachPtrInRangeIdx by modifying vector during iteration
    bool test_vec_foreach_ptr_in_range_idx_out_of_bounds_access(void) {
    WriteFmt("Testing VecForeachPtrInRangeIdx where idx goes out of bounds (should crash)\n");
    
    typedef Vec(int) IntVec;
    // Use VecForeachPtrInRangeIdx with a fixed range that becomes invalid when we modify the vector
    size original_length = vec.length; // Capture this as 9
    VecForeachPtrInRangeIdx(&vec, val_ptr, idx, 0, original_length) {
    WriteFmt("Accessing idx {} (vec.length={}): {}\n", idx, vec.length, *val_ptr);
    /// TAGS: Foreach, Vec, Iteration, Loop, Range, Pointer
    ///
    #define VecForeachPtrInRange(v, var, start, end) VecForeachPtrInRangeIdx((v), (var), (____iter___), (start), (end))
    
    #endif // MISRA_STD_CONTAINER_VEC_FOREACH_H
    ///
    #define StrForeachPtrInRangeIdx(str, chrptr, idx, start, end)                                                          \
    VecForeachPtrInRangeIdx((str), (chrptr), idx, (start), (end))
    
    ///

Share :

Related Posts

VecForeach

VecForeach Description Iterate over each element var of the given vector v. This is a convenience macro that iterates forward using an internally managed index. The variable var is declared and defined by this macro.

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

VecForeachPtrIdx

VecForeachPtrIdx Description Iterate over each element var of given vector v at each index idx into the vector. The variables var and idx declared and defined by this macro. idx will start from 0 and will go till v->length - 1

Read More