VecResize
Description
Resize the vector to exactly len elements. Truncates when shrinking and allocates when growing. New elements (when growing) are zero-initialized.
Parameters
| Name | Direction | Description |
|---|---|---|
v |
in,out | Vector handle. |
len |
in | New length. |
Success
Returns true. The vector length is now exactly len. When shrinking, the elements beyond len were deinitialized via the configured copy_deinit (if any) and dropped. When growing, the new slots in [old_length, len) are zero-initialized; the allocated capacity is at least len.
Failure
Returns false on allocation failure when growth is needed. The vector is unchanged (length, capacity, and elements all preserved). Shrinking does not allocate and therefore cannot fail.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
VecCharPtr.c:167:
// VecResize automatically handles cleanup of removed elements and
// initializes new elements to NULL for pointer types
VecResize(vec, new_size);
// Fill new slots with generated strings if vector grew.
- In
VecStr.c:189:
// VecResize automatically handles cleanup of removed elements and
// initializes new elements to default (empty Str)
VecResize(vec, new_size);
// Initialize new Str objects if vector grew
- In
VecInt.c:131:
uint16_t new_size = extract_u16(data, offset, data_size);
new_size = new_size % 1000; // Limit to reasonable size
VecResize(vec, new_size);
break;
} // After 2nd iteration, shrink the vector dramatically
if (iteration_count == 2) {
VecResize(&vec, 2); // Shrink to only 2 elements
WriteFmt("Vector resized to length {} during foreach iteration...\n", VecLen(&vec));
} // 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)
WriteFmt("Vector resized to length {}, current idx={} is now out of bounds...\n", VecLen(&vec), idx);
} // but the vector length is now smaller
if (idx == 4) {
VecResize(&vec, 2); // Shrink to only 2 elements
WriteFmt("Vector resized to length {} during reverse iteration...\n", VecLen(&vec));
} // 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)
WriteFmt("Vector resized to length {}, current idx={} is now out of bounds...\n", VecLen(&vec), idx);
} // When we reach idx=5, shrink the vector significantly
if (idx == 5) {
VecResize(&vec, 3); // Shrink to only 3 elements
WriteFmt("Vector resized to length {} during reverse ptr iteration...\n", VecLen(&vec));
} // This will make subsequent iterations invalid
if (idx == 2) {
VecResize(&vec, 1); // Shrink to only 1 element
WriteFmt("Vector resized to length {}, current index={}\n", VecLen(&vec), idx);
} bool result = true;
VecResize(&patterns, 3);
// Create source: 110101
bool result = true;
VecResize(&patterns, 3);
// Create source: 110101
- In
Vec.Memory.c:57:
// Test VecResize function
bool test_vec_resize(void) {
WriteFmt("Testing VecResize\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Vec.Memory.c:75:
// Resize to a smaller length
VecResize(&vec, 3);
// Length should now be 3
- In
Vec.Memory.c:86:
// Resize to a larger length
VecResize(&vec, 8);
// Length should now be 8
- In
Buf.h:121:
/// TAGS: Buf, Resize, Capacity, Allocation
///
#define BufResize(b, n) VecResize((b), (n))
///
- In
Memory.h:76:
#define VecMustResize(v, len) \
do { \
if (!VecResize((v), (len))) { \
LOG_FATAL("VecResize failed"); \
} \- In
Memory.h:77:
do { \
if (!VecResize((v), (len))) { \
LOG_FATAL("VecResize failed"); \
} \
} while (0)- In
Memory.h:51:
/// TAGS: Str, Memory, Resize
///
#define StrResize(str, len) VecResize((str), (len))
///
Last updated on