I have this RPC structure T_Struct
that came from the wire.
I would like to make a copy of it, but I do not want to write a separate function to deal with all structures, allocations, and arrays of its members (especially that I will have to do the same thing for tones of other structures.)
Since I have already a way to decode, encode, and free, would it make sense to have something like that:
void copy_T_Struct( T_Struct* destination, T_Struct* source )
{
XDR xdr ;
/* Is there a way I can know the size of the buffer for the struct? */
char buffer[ 10240 ] ;
xdrmem_create( &xdr, buffer, sizeof( buffer ), XDR_ENCODE ) ;
( *xdr_T_Struct )( &xdr, source ) ; /* serialize to buffer */
xdr.x_op = XDR_DECODE ;
memset( destination, 0, sizeof( *destination )) ; /* without it I see segfault */
( *xdr_T_Struct )( &xdr, destination ) ; /* serialize back to T_Struct */
xdr_destroy( &xdr ) ;
}
I understand that at the end, I will also be able to call xdr_free((xdrproc_t)xdr_T_Struct, (char *)destination ) ;
Here is the final solution. Notice that this is a C version.
Uses static reallocatable buffer. Example of us below.
xdr_copy.h
xdr_copy.c
Example