VecForeach
- Macro
- October 8, 2025
Table of Contents
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.
Parameters
Name | Direction | Description |
---|---|---|
v | in,out | Vector to iterate over. |
var | in | Name of the variable to be used which will contain the value of the current element during iteration. The type of var will be the data type of the vector elements (obtained via VEC_DATATYPE(v) ). |
Usage example (Cross-references)
- In
Vec.Foreach.c:30
:
// Test VecForeach macro
bool test_vec_foreach(void) {
WriteFmt("Testing VecForeach\n");
// Create a vector of integers
- In
Vec.Foreach.c:44
:
// Use VecForeach to sum the values
int sum = 0;
VecForeach(&vec, item) {
sum += item;
}
- In
Vec.Foreach.c:52
:
// Use VecForeach to double each value
VecForeach(&vec, item) {
item *= 2;
}
// Make idx go out of bounds during VecForeach by modifying vector during iteration
bool test_vec_foreach_out_of_bounds_access(void) {
WriteFmt("Testing VecForeach where modification causes out of bounds access (should crash)\n");
typedef Vec(int) IntVec;
// VecForeach doesn't use an explicit index but we can still cause issues
int iteration_count = 0;
VecForeach(&vec, val) {
WriteFmt("Iteration {} (vec.length={}): {}\n", iteration_count, vec.length, val);
- In
Read.Nested.c:82
:
StrDeinit(&result->binary_name);
StrDeinit(&result->sha256);
VecForeach(&result->tags, tag) {
StrDeinit(&tag);
}
JW_OBJ(json, {
JW_OBJ_KV(json, "functions", {
VecForeach(&symbols, symbol) {
Str source_key = StrInit();
StrWriteFmt(&source_key, "{}", symbol.source_function_id);
- In
MisraEnum.c:116
:
// Use VecForeach for iterating over entries
VecForeach(&entries, e) {
if (last_value == e.value - 1) {
StrWriteFmt(&code, " {},\n", e.name.data);
- In
MisraEnum.c:153
:
// Use VecForeach for iterating over entries
VecForeach(&entries, e) {
const char *compareTemplate = " if(ZstrCompareN(\"{}\", zstr, {}) == 0) {{return {};}}\n";
// Store the length in a variable to avoid taking address of rvalue
- In
MisraEnum.c:178
:
// Use VecForeach for iterating over entries
VecForeach(&entries, e) {
const char *caseTemplate = " case {} : {{return \"{}\";}}\n";
StrWriteFmt(&code, caseTemplate, e.name.data, e.str.data);
- In
MisraEnum.c:212
:
StrDeinit(&code);
VecForeach(&entries, e) {
StrDeinit(&e.name);
StrDeinit(&e.str);
- In
MisraDoc.c:129
:
// recursively explore directories and get filenames
VecForeach(&dir_paths, dir_name) {
// keep track of current path we're exploring
Str current_path = StrInit();
- In
MisraDoc.c:136
:
SysDirContents dir_contents = SysGetDirContents(dir_name.data);
Scope(&dir_contents, VecDeinit, {
VecForeach(&dir_contents, dir_entry) {
// if it's a directory then store it for exploration later on
if (dir_entry.type == SYS_DIR_ENTRY_TYPE_DIRECTORY) {
- In
MisraDoc.c:165
:
// go over each file and generate corresponding markdown
VecForeach(&file_paths, file_path) {
Str file_contents = StrInit();
Scope(&file_contents, StrDeinit, {
- In
JSON.h:733
:
(void)___is_first___; \
StrPushBack(&(j), '['); \
VecForeach(&(arr), item) { \
if (___is_first___) { \
___is_first___ = false; \
- In
Foreach.h:80
:
/// the character type used by the `Str` implementation (e.g., `char`).
///
#define StrForeach(str, chr) VecForeach((str), (chr))
///
- In
VecInt.c:399
:
if (VecLen(vec) > 0) {
int sum = 0;
VecForeach(vec, item) {
sum += item;
}
- In
VecStr.c:429
:
if (VecLen(vec) > 0) {
size_t total_len = 0;
VecForeach(vec, str) {
total_len += ZstrLen(str.data);
}
- In
VecCharPtr.c:467
:
if (VecLen(vec) > 0) {
size_t total_len = 0;
VecForeach(vec, str) {
total_len += strlen(str);
}