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).

Success

The body is executed for each element of the vector v from the start index to the end-1 index, with var pointing to each element.

Failure

If the vector v is NULL, its length is zero, or the range is invalid, the loop body will not be executed. Any access to an invalid index will result in a fatal log message and program termination.

Usage example (Cross-references)

    ///
    #define StrForeachPtrInRangeIdx(str, chrptr, idx, start, end, body)                                                    \
    VecForeachPtrInRangeIdx((str), (chrptr), idx, (start), (end), {body})
    
    ///
    ///
    #define VecForeachPtrInRange(v, var, start, end, body)                                                                 \
    VecForeachPtrInRangeIdx((v), (var), (____iter___), (start), (end), {body})
    
    #endif // MISRA_STD_CONTAINER_VEC_FOREACH_H
    // Deadend test: 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) {
    printf("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, {
    printf("Accessing idx %zu (vec.length=%zu): %d\n", idx, vec.length, *val_ptr);

Share :