Skip to content

VecClear

Description

Set the vector length to 0 while keeping the allocated capacity. Element payloads are deinitialized via the configured copy_deinit handler when present.

Parameters

Name Direction Description
v in,out Vector handle.

Success

Returns to the caller. The vector length is now 0; the allocated capacity and data buffer are preserved. When copy_deinit is configured it has been invoked on each previously-stored element.

Failure

Function cannot fail.

Usage example (Cross-references)

Usage examples (Cross-references)
    /// TAGS: Buf, Clear, Reuse
    ///
    #define BufClear(b)  VecClear(b)
    
    // Read-only accessors. The leading `((void)0, ...)` makes each macro a
    /// TAGS: Str, Memory, Clear
    ///
    #define StrClear(str) VecClear(str)
    
    ///
            // Memory operations
            case VEC_INT_CLEAR : {
                VecClear(vec);
                break;
            }
            case VEC_CHAR_PTR_CLEAR : {
                // VecClear automatically calls char_ptr_deinit on each element
                VecClear(vec);
                break;
            }
            case VEC_STR_CLEAR : {
                // VecClear automatically calls StrDeinit on each element
                VecClear(vec);
                break;
            }
            LOG_FATAL("buf_write_fmt: NULL out or fmtstr");
        }
        VecClear(out);
        return render_binary_fmt((Str *)out, fmtstr, argv, argc);
    }
    
        // Test with a vector of even length
        VecClear(&vec);
        int even_values[] = {10, 20, 30, 40};
        for (int i = 0; i < 4; i++) {
    // Test VecClear function
    bool test_vec_clear(void) {
        WriteFmt("Testing VecClear\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Clear the vector
        VecClear(&vec);
    
        // Length should now be 0
    // Str the buffer -- including the NUL sentinel -- must be zeroed.
    bool test_clear_str_scrubs_sentinel(void) {
        WriteFmt("Testing VecClear scrubs Str sentinel byte\n");
    
        DefaultAllocator local = DefaultAllocatorInit();
    // mutation. A one-character Str has capacity == 1 exactly.
    bool test_clear_str_cap1_scrubs_sentinel(void) {
        WriteFmt("Testing VecClear scrubs single-char Str (capacity 1)\n");
    
        DefaultAllocator local = DefaultAllocatorInit();
    // clear_vec @ 84:18 / 84:20 (scrub MemSet stride forced to 42).
    bool test_clear_large_char_vec_scrub_stride(void) {
        WriteFmt("Testing VecClear scrub stride on a large char vector\n");
    
        DefaultAllocator local = DefaultAllocatorInit();
        }
    
        VecClear(&vec);
    
        bool result = (VecLen(&vec) == 0);
    // once per live element.
    bool test_clear_runs_deinit_once_per_element(void) {
        WriteFmt("Testing VecClear invokes copy_deinit once per element\n");
    
        DefaultAllocator local = DefaultAllocatorInit();
    
        g_deinit_calls = 0;
        VecClear(&vec);
    
        bool result = (g_deinit_calls == 3) && (VecLen(&vec) == 0);
Last updated on