Skip to content

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)
    /// TAGS: Buf, Resize, Capacity, Allocation
    ///
    #define BufResize(b, n)  VecResize((b), (n))
    
    ///
    #define VecMustResize(v, len)                                                                                          \
        do {                                                                                                               \
            if (!VecResize((v), (len))) {                                                                                  \
                LOG_FATAL("VecResize failed");                                                                             \
            }                                                                                                              \
        do {                                                                                                               \
            if (!VecResize((v), (len))) {                                                                                  \
                LOG_FATAL("VecResize failed");                                                                             \
            }                                                                                                              \
        } while (0)
    /// TAGS: Str, Memory, Resize
    ///
    #define StrResize(str, len) VecResize((str), (len))
    
    ///
                uint16_t new_size = extract_u16(data, offset, data_size);
                new_size          = new_size % 1000; // Limit to reasonable size
                VecResize(vec, new_size);
                break;
            }
                    // 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.
                    // 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
    // Test VecResize function
    bool test_vec_resize(void) {
        WriteFmt("Testing VecResize\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Resize to a smaller length
        VecResize(&vec, 3);
    
        // Length should now be 3
    
        // Resize to a larger length
        VecResize(&vec, 8);
    
        // Length should now be 8
    
        // Grow well past the current capacity.
        bool ok = VecResize(&vec, 100);
    
        bool result = ok && (VecLen(&vec) == 100) && (VecCapacity(&vec) >= 100);
        }
    
        VecResize(&vec, 1);
    
        bool result = (VecLen(&vec) == 1) && (g_deinit_count == 3);
        }
    
        VecResize(&vec, 64);
    
        // Whichever way the constant forces the branch, a real grow must leave
        }
    
        VecResize(&vec, 50);
    
        bool result = (VecLen(&vec) == 50);
    
        // Empty -> grow branch -> reserve_pow2_vec(10). Real: next pow2 >= 10 == 16.
        VecResize(&vec, 10);
    
        bool result = (VecLen(&vec) == 10) && (VecCapacity(&vec) == 16);
    
        // 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;
        // 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
            // 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
Last updated on