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.
c ++ c pointers memcpy
Ilya Suzdalnitski
source share