VecResize

Table of Contents

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

NameDirectionDescription
vecin,outVector to be resized.
leninNew length of vector.

Success

return

Failure

Does not return

Usage example (Cross-references)

    /// FAILURE : Does not return
    ///
    #define StrResize(str, len) VecResize((str), (len))
    
    ///
    // Test VecResize function
    bool test_vec_resize(void) {
    printf("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
    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

Share :