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
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))
///
- 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;
}- 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
Memory.c:122:
// Test VecResize function
bool test_vec_resize(void) {
WriteFmt("Testing VecResize\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Memory.c:140:
// Resize to a smaller length
VecResize(&vec, 3);
// Length should now be 3
- In
Memory.c:151:
// Resize to a larger length
VecResize(&vec, 8);
// Length should now be 8
- In
Memory.c:273:
// Grow well past the current capacity.
bool ok = VecResize(&vec, 100);
bool result = ok && (VecLen(&vec) == 100) && (VecCapacity(&vec) >= 100);- In
Memory.c:296:
}
VecResize(&vec, 1);
bool result = (VecLen(&vec) == 1) && (g_deinit_count == 3);- In
Memory.c:317:
}
VecResize(&vec, 64);
// Whichever way the constant forces the branch, a real grow must leave
- In
Memory.c:339:
}
VecResize(&vec, 50);
bool result = (VecLen(&vec) == 50);- In
Memory.c:356:
// Empty -> grow branch -> reserve_pow2_vec(10). Real: next pow2 >= 10 == 16.
VecResize(&vec, 10);
bool result = (VecLen(&vec) == 10) && (VecCapacity(&vec) == 16);- In
Memory.c:388:
// Live data in [0,8); resize re-zeroes only the sentinel slot.
VecResize(&vec, 8);
for (u64 i = 0; i < VecLen(&vec); i++) {
VecAt(&vec, i) = 0x11111111;- In
Memory.c:396:
// the newly in-range region [8,12).
VecReserve(&vec, 12);
VecResize(&vec, 12);
// Live data below the old capacity is preserved; the probed grown slot must
- In
Foreach.c:325:
// 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));
}- In
Foreach.c:365:
// 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);
}- In
Foreach.c:404:
// 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));
}- In
Foreach.c:442:
// 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);
}- In
Foreach.c:479:
// 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));
}- In
Foreach.c:555:
// 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
Last updated on