Skip to content

VecInitClone

Description

Reinitialize vd as a deep clone of vs. Any current contents of vd are first deinitialized. The destination adopts vs’s copy_init / copy_deinit / allocator configuration, then all elements are deep-copied.

Parameters

Name Direction Description
vd out Destination vector. Must be initialized; its current contents are released before cloning.
vs in Source vector.

Success

Returns true. vd now holds a deep copy of every element of vs, has the same length, and carries vs’s copy_init / copy_deinit / allocator configuration. The prior contents of vd were released before the clone began.

Failure

Returns false on allocation failure during the clone. vd is left in a valid but partially-populated state (the prior contents are gone). Callers should treat vd as opaque on failure and call VecDeinit(vd) before reuse.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define VecMustInitClone(vd, vs)                                                                                       \
        do {                                                                                                               \
            if (!VecInitClone((vd), (vs))) {                                                                               \
                LOG_FATAL("VecMustInitClone failed");                                                                      \
            }                                                                                                              \
    /// TAGS: Str, Clone, Init, DeepCopy
    ///
    #define StrInitClone(strd, strs) VecInitClone((strd), (strs))
    
    ///
                }
    
                VecInitClone(vec, &temp);
    
                VecDeinit(&temp); // Clean up temp to prevent memory leak
                    }
    
                    VecInitClone(vec, &temp);
    
                    VecDeinit(&temp);
                    }
    
                    VecInitClone(vec, &temp);
    
                    VecDeinit(&temp);
    
        IntVec dst = VecInit(&local);
        VecInitClone(&dst, &src);
    
        // Under the mutant src->length != 0 returns true immediately and dst is
    
        IntVec dst = VecInit(&local);
        VecInitClone(&dst, &src);
    
        // Real: dst has all 3 elements starting at index 0. Mutant (init 42):
    
        IntVec dst = VecInit(&local);
        VecInitClone(&dst, &src);
    
        // Mutant (i >= length): 0 >= 3 is false, loop never runs, dst empty.
    
        IntVec dst = VecInit(&local);
        VecInitClone(&dst, &src);
    
        // Real: dst length 3, all values present. Mutant (i--): dst length 1.
    
        IntVec dst    = VecInit(&local);
        bool   cloned = VecInitClone(&dst, &src);
    
        // Real: cloned true, dst length 3. Mutant: insert expression is 0 so the
    
        U32Vec dst = VecInit(&alloc);
        VecInitClone(&dst, &src);
    
        // Real: dst == {10,20,30,40}. Mutant (constant offset): every source read
Last updated on