将结构深层复制到另一个 [英] Deep copy a struct to another

查看:66
本文介绍了将结构深层复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 struct ,其中包含字符串和指针.是否有任何库函数可用于将 struct 的深层副本复制到另一个库中.我不想按字段进行复制,因为我拥有的结构很大.

I have a struct which contains strings and pointer within. Is there any library functions available to do a deep copy of the struct into another. I don't want to do a field by field copy since the struct I'm having is quite large.

glib是否具有实现该功能的任何功能?

Does glib have any function that does the trick?

推荐答案

您可以使用memcpy或memmove复制结构本身的全部内容.但是,由于C没有自省功能,因此通用函数无法完成对指向对象的复制.

You can use memcpy or memmove to copy the entire contents of the struct itself. However, as C has no introspection, copying pointed-to objects can't be done by a general purpose function.

编辑添加:正如一些评论者所指出的,您可以将结构分配给最近二十年来使用的C语言方言中的其他结构,不再需要memcpy.

Edited to add: As several commenters note, you can just assign structures to other structures in the C dialects in use for the last couple of decades, memcpy is not needed any longer.

这篇关于将结构深层复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆