romi1013
romi1013

Reputation: 53

deep copy of struct with Pointer Point in C

i need your help!

I like to copy a struct like this:

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));

I try it like this:

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;
}

How can i solve this?

Thank you in advance for your help!!

UPDATE - my solution for deepcopy of vertex - thx for all help:

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)); 
}

Upvotes: 5

Views: 3804

Answers (3)

Foon
Foon

Reputation: 6433

Original answer:

Assuming vertex points to an array of vertices, and that each vertice contains 3 longs (x,y,z):

dst->vertex = (long **)malloc(dst->num_vertex * 3 * sizeof(long);
memcpy(dst,src,dst->num_vertex * 3 * sizeof(long));  

Update because I realized this might work but isn't clean or especially safe As I mentioned in comments, code would be cleaner if you had

typedef struct vertextag { 
  long x;
  long y;
  long z;
} vertex_type;

And then did: dst->vertex = (vertex_type *)malloc(dst->num_vertex * sizeof(vertex_type); memcpy(dst,src,dst->num_vertex * sizeof(vertex_type));

Upvotes: 2

Sparky
Sparky

Reputation: 14057

I'm going to assume that the vertices are not shared between objects. That is, they belong to the structure in question.

There are two primary cases to consider:

1. Copying into a new object
2. Copying into an existing object

Copying into the new object is straightforward.

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.

Copying into an existing object is much the same as copying into a new object except that you have to do the following first.

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.

Hope this helps.

Upvotes: 2

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

It depends on whether the vertex-array should belong to the object you are trying to copy or whether it is shared among multiple objects. Both approaches are used in practice, depending on the situation (the array must be copied if the vertices can be changed for the copy-object separately). You have to choose which of them makes sense for the application you are developing.

If the array can be shared by objects pointing to them, just copy the pointer.

dst->vertex = src->vertex;

If each object has their own vertices (so they can be changed separately for the copied object) then you have to allocate and copy the array and the place where the pointer is stored and set a pointer to that place into the copy-object.

long* vertexCopy = malloc(howmanybytes);
memcpy(vertexCopy, *src->vertex, howmanybytes);
long** holder = malloc(sizeof(void*));
holder[0] = vertexCopy;
dst->vertex = holder;

Upvotes: 1

Related Questions