我需要你的帮助!
我喜欢复制这样的结构:
typedef struct PackageObject_s {
long **vertex; // vertices
long num_vertex; // count of vertices
long objectType; //
REAL r; //
long bottom[3]; // bounding box bottom vector
long top[3]; // bounding box top vector
long *start; //
REAL coverage; //
} PackageObject __attribute__ ((aligned));
我就这样试试:
static inline void PackageObject_copy(PackageObject *dst, const PackageObject *src) {
dst->num_vertex = src->num_vertex;
dst->objectType = src->objectType;
dst->r = src->r;
vec_assign3l(dst->bottom, src->bottom);
vec_assign3l(dst->top, src->top);
// TODO copy **vertex ???
dst->coverage = src->coverage;
dst->coverage = src->coverage;
}
我该怎么解决?
提前谢谢你的帮助!!
更新-所有帮助的
vertex
-thx的deepcopy的“我的解决方案”:dst->vertex = (long *)malloc(dst->num_vertex * 3 * sizeof(long));
for (long i=0; i < src->num_vertex; i++) {
dst->vertex[i] = (long)malloc(3*sizeof(long));
memcpy(dst->vertex[i],src->vertex[i],3 * sizeof(long));
}
最佳答案
我假设顶点不是在对象之间共享的。也就是说,它们属于所讨论的结构。
有两种主要情况需要考虑:
1. Copying into a new object
2. Copying into an existing object
复制到新对象很简单。
1a. Allocate space for <num_vertex> pointers.
1b. Allocate space for each vertex.
2a. Copy <num_vertex> pointers from source to destination.
2b. Copy <num_vertex> vertices from source to destination.
复制到现有对象与复制到一个新对象几乎相同,除非您必须先做以下操作。
0a. Loop through each element of <vertex> and free the vertex.
0b. Free the array of vertex pointers.
1. Follow the steps for copying into a new object.
希望这有帮助。