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)

    // 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))
    
    ///
    if (start < end) {
    int sum = 0;
    VecForeachPtrInRangeIdx(vec, item_ptr, idx, start, end) {
    sum += *item_ptr + (int)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) {
    size_t total_len = 0;
    VecForeachPtrInRangeIdx(vec, str_ptr, idx, start, end) {
    total_len += strlen(*str_ptr) + idx;
    }

Share :

Related Posts

VecForeachPtrReverse

VecForeachPtrReverse Description Iterate over each element var (as a pointer) of the given vector v in reverse order. This is a convenience macro that iterates backward using an internally managed index and provides a pointer to each element. The variable var is declared and defined by this macro as a pointer to the vector’s data type.

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

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