Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)); 
}
like image 867
romi1013 Avatar asked Feb 17 '13 17:02

romi1013


People also ask

How do you deep copy a struct?

To perform a deep copy you must first free any memory that was being pointed to by the destination structure. Then allocate enough memory to hold the strings pointed to by the source structure. Now, strncpy the strings over. Save this answer.

Can I copy a struct in C?

We can also use assignment operator to make copy of struct. A lot of people don't even realize that they can copy a struct this way because one can't do same it with an array. Similarly one can return struct from a function and assign it but not array.

What is a deep copy in C?

1. When we create a copy of object by copying data of all member variables as it is, then it is called shallow copy. When we create an object by copying data of another object along with the values of memory resources that reside outside the object, then it is called a deep copy.

What is the difference between shallow and deep copying in pointers?

In Shallow copy, a copy of the original object is stored and only the reference address is finally copied. In Deep copy, the copy of the original object and the repetitive copies both are stored.


2 Answers

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.

like image 73
Sparky Avatar answered Sep 22 '22 15:09

Sparky


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

like image 30
Foon Avatar answered Sep 21 '22 15:09

Foon