VecResize
- Macro
- August 22, 2025
Table of Contents
VecResize
VecResize
Description
Resize vector. If length is smaller than current capacity, vector length is shrinked. If length is greater than current capacity, space is reserved and vector is expanded.
Parameters
Name | Direction | Description |
---|---|---|
vec | in,out | Vector to be resized. |
len | in | New length of vector. |
Success
return
Failure
Does not return
Usage example (Cross-references)
- In
Memory.h:52
:
/// FAILURE : Does not return
///
#define StrResize(str, len) VecResize((str), (len))
///
- In
Vec.Memory.c:54
:
// Test VecResize function
bool test_vec_resize(void) {
printf("Testing VecResize\n");
// Create a vector of integers
- In
Vec.Memory.c:70
:
// Resize to a smaller length
VecResize(&vec, 3);
// Length should now be 3
- In
Vec.Memory.c:81
:
// Resize to a larger length
VecResize(&vec, 8);
// Length should now be 8
// After 2nd iteration, shrink the vector dramatically
if (iteration_count == 2) {
VecResize(&vec, 2); // Shrink to only 2 elements
printf("Vector resized to length %zu during foreach iteration...\n", vec.length);
}
// The bounds check happens after the body, so it will check if idx=2 >= new_length
if (idx == 2) {
VecResize(&vec, 2); // Shrink so that idx=2 becomes out of bounds (valid indices: 0,1)
printf("Vector resized to length %zu, current idx=%zu is now out of bounds...\n", vec.length, idx);
}
// but the vector length is now smaller
if (idx == 3) {
VecResize(&vec, 2); // Shrink to only 2 elements
printf("Vector resized to length %zu during reverse iteration...\n", vec.length);
}
// The bounds check happens after the body, so it will check if idx=3 >= new_length
if (idx == 3) {
VecResize(&vec, 3); // Shrink so that idx=3 becomes out of bounds (valid indices: 0,1,2)
printf("Vector resized to length %zu, current idx=%zu is now out of bounds...\n", vec.length, idx);
}
// When we reach idx=5, shrink the vector significantly
if (idx == 5) {
VecResize(&vec, 3); // Shrink to only 3 elements
printf("Vector resized to length %zu during reverse ptr iteration...\n", vec.length);
}
// This will make subsequent iterations invalid
if (idx == 2) {
VecResize(&vec, 1); // Shrink to only 1 element
printf("Vector resized to length %zu, but basic foreach iteration continues...\n", vec.length);
}
bool result = true;
VecResize(&patterns, 3);
// Create source: 110101
bool result = true;
VecResize(&patterns, 3);
// Create source: 110101