【发布时间】:2014-11-18 17:23:45
【问题描述】:
我有这个来自网络的 RPC 结构T_Struct。
我想复制它,但我不想编写一个单独的函数来处理其成员的所有结构、分配和数组(尤其是我必须对其他结构的音调做同样的事情。 )
既然我已经有了解码、编码和释放的方法,那么有这样的东西是否有意义:
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 ) ;
}
我知道最后我也可以拨打xdr_free((xdrproc_t)xdr_T_Struct, (char *)destination ) ;
【问题讨论】:
-
看起来它可以正常工作。需要添加的是在 XDR_ENCODE-ing 时检查 xdr_T_Struct() 函数调用的输出,并在失败时增加缓冲区大小。