Skip to content

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)

Usage examples (Cross-references)
                    // 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
                uint16_t new_size = extract_u16(data, offset, size);
                new_size          = new_size % 1000; // Limit to reasonable size
                VecResize(vec, new_size);
                break;
            }
    // Test VecResize function
    bool test_vec_resize(void) {
        WriteFmt("Testing VecResize\n");
    
        // Create a vector of integers
    
        // 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
            // 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", 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)
                WriteFmt("Vector resized to length {}, current idx={} is now out of bounds...\n", vec.length, 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", 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)
                WriteFmt("Vector resized to length {}, current idx={} 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
                WriteFmt("Vector resized to length {} during reverse ptr iteration...\n", vec.length);
            }
            // 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", vec.length, idx);
            }
        bool    result   = true;
    
        VecResize(&patterns, 3);
    
        // Create source: 110101
        bool    result   = true;
    
        VecResize(&patterns, 3);
    
        // Create source: 110101
    /// FAILURE : Does not return
    ///
    #define StrResize(str, len) VecResize((str), (len))
    
    ///
Last updated on