VecForeachPtrReverseIdx
- Macro
- August 22, 2025
Table of Contents
VecForeachPtrReverseIdx
VecForeachPtrReverseIdx
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 v->length - 1 and will go till 0
Parameters
Name | Direction | Description |
---|---|---|
v | in,out | Vector to iterate over. |
var | in | Name of variable to be used which’ll contain value at iterated index idx |
idx | in | Name of variable to be used for iterating over indices. |
Usage example (Cross-references)
- In
Foreach.h:72
:
/// body : Body of this foreach loop.
///
#define StrForeachReversePtrIdx(str, chrptr, idx, body) VecForeachPtrReverseIdx((str), (chrptr), idx, {body})
///
- In
Foreach.h:205
:
/// invalid index access) will result in a fatal log message and program termination.
///
#define VecForeachPtrReverse(v, var, body) VecForeachPtrReverseIdx((v), (var), (____iter___), {body})
///
// Test VecForeachPtrReverseIdx macro
bool test_vec_foreach_ptr_reverse_idx(void) {
printf("Testing VecForeachPtrReverseIdx\n");
// Create a vector of integers
// Use VecForeachPtrReverseIdx to set each value to its index + 100
VecForeachPtrReverseIdx(&vec, item_ptr, idx, { *item_ptr = idx + 100; });
// Check that the values are set correctly
// Deadend test: Make idx go out of bounds in VecForeachPtrReverseIdx by modifying vector during iteration
bool test_vec_foreach_ptr_reverse_idx_out_of_bounds_access(void) {
printf("Testing VecForeachPtrReverseIdx where idx goes out of bounds (should crash)\n");
typedef Vec(int) IntVec;
// VecForeachPtrReverseIdx has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
VecForeachPtrReverseIdx(&vec, val_ptr, idx, {
printf("Accessing idx %zu (vec.length=%zu): %d\n", idx, vec.length, *val_ptr);