C ++: Will the structure be copied correctly?

I have a pointer to a structure, and I need to implement a method that copies the entire contents of the structure's memory. Generally speaking, I need to do a deep copy of the structure.

Here's the structure:

typedef struct { Size2f spriteSize; Vertex2f *vertices; GLubyte *vertex_indices; } tSprite; 

And here is the method that I implemented should copy the structure:

 tSprite* copySprite(const tSprite *copyFromMe) { tSprite *pSpriteToReturn = (tSprite*)malloc( sizeof(*copyFromMe) ); memcpy(pSpriteToReturn, copyFromMe, sizeof(*copyFromMe) ); return pSpriteToReturn; } 

The problem is that I’m not sure that the arrays “vertices” and “index_ vertices” will be copied correctly. What will be copied in this way? The address of the array or the array itself?

Should I copy arrays after copying the structure? Or is it simple enough to copy the structure?

Something like that:

 ... pSpriteToReturn->vertices = (Vector2f*)malloc( sizeof(arraysize) ); memcpy(pSpriteToReturn->vertices, copyFromMe->vertices, sizeof(arraysize) ); ... 

Thanks in advance.

+7
c ++ c pointers memcpy
source share
7 answers

As a rule, never use memcpy in C ++ in normal code (it can occur in very low-level code, for example, in dispensers) 1) . Instead, create a suitable copy constructor and overload operator = (assignment operator) to match it (and the destructor rule is three: "if you implement any of the copy constructors, operator = and destructor, you must implement all three).

If you do not implement your own versions of the copy constructor, the assignment operator, C ++ will create the default versions for you. In these versions a shallow copy will be implemented (to make memcpy ), i.e. In your case, the contents of the array will not be copied - only pointers.


1) By the way, the same thing happens for malloc and free . Do not use them, use new / new[] and delete / delete[] instead.

+8
source share

This partly depends on your requirements. If you do not copy arrays, both structures will point to the same array, which may or may not be a problem.

+3
source share

Your schema will copy the addresses of the arrays. The returned instance of "copy" tSprite will have pointers to the same data (in memory) as the ones passed in one.

If you need a genuine deep copy, you need to copy the arrays (and any elements of their elements) manually.

+3
source share

If this is the C ++ you are writing to, remember that C ++ has new and delete for some reason. As for the question itself, it depends on whether you want to copy pointers or the structures themselves. If the latter, you also need to copy them!

+2
source share

This is not the right way to copy, even if you are running simple C.

A specified in another answer, you will get two (or more) instances of the structure pointing to the same instance of Vertext2 and GLubyte , which is not recommended.

This will lead to problems, for example, who will free the memory allocation for Vertext2 GLubyte

Should I copy the arrays after copying the structure? Or is it enough just to copy the structure?

Yes, this is the right way to do it.

+1
source share

The pointers themselves will be copied, but this means that both “from” and “to” will be the same in two sprites. You will also need to manually select and copy the things that the pointers point to, but that means you also need to know how large the arrays that the pointers refer to are.

Note that instead of memcpy there you can also do '* pSpriteToReturn = * copyFromMe;' This will copy all members, although if you are going to create new arrays, the only part of tSprites that you want to copy is size.

One more note: if your sprites always have a fixed number of vertices and vertex indices, you can create these arrays inside the sprite, not pointers. If you do this, they will be correctly copied with both the memcpy method and the assignment that I mentioned in the previous paragraph.

+1
source share

in C ++, new and delete are allocated to the heap.

 Sprite *ptr =...; Sprite *s = new Stripe(*ptr); // copy constructor, shallow copy off pointers s->member = new Member(*ptr->member); // copy construct sprite member s->array = new int[4]; //allocate array std::copy(ptr-> array, ptr->array + 4, s->array); //copy array delete[] s->array; //delete array, must use delete[] 
+1
source share

All Articles