Functions

AClone

Duplicate a nested or multidimensional array

Syntax

Signature
ITEM *AClone( const ITEM *source );

Arguments

source Array to duplicate.

Return value

A new array, which ItemRelease() frees, or NULL if source is not an array.

Description

Use `AClone()` when a second array needs independent element storage, including independent storage for nested rows. It copies values and recursively duplicates subarrays. `ACopy()` and `ItemCopy()` preserve references to the original subarrays.

The clone preserves sharing within the source structure. If two elements refer to the same subarray, the cloned elements refer to one shared copy. A self-reference is reproduced in the copy. A reference cycle keeps the arrays in that cycle alive after the outer item is released.

Example

aclone.c
#include "xdev-wtui.h"

#include <stdio.h>

static void printNumbers( const char *title, const ITEM *array )
{
   printf( "%s:", title );
   for( size_t i = 1; i <= ( size_t )ArrayLen( array ); ++i )
      printf( " %d", ArrayGetNI( array, i ) );

   printf( "\n" );
}

int main( void )
{
   ITEM *one = Array( 3 );
   ITEM *two;

   if( !one )
      return 1;

   for( size_t i = 1; i <= 3; ++i )
      ArraySetNI( one, i, ( int )i );

   two = AClone( one );
   ArraySetNI( one, 1, 99 );

   printNumbers( "One", one );
   printNumbers( "Two", two );

   ItemRelease( two );
   ItemRelease( one );

   return 0;
}

Output

One: 99 2 3
Two: 1 2 3

See also