Hello,
I have a pointer to a structure and I need to implement a method that will copy all of the memory contents of a structure. Generally speaking I need to perform a deep copy of a structure.
Here's the structure:
typedef struct {
Size2f spriteSize;
Vertex2f *vertices;
GLubyte *vertex_indices;
} tSprite;
And here's the method I've implemented that 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 arrays "vertices" and "vertex_indices" are going to be copied properly. What is going to be copied in this way? Address of the array or the array itself?
Should I copy the arrays after copying the structure? Or is it enough just to copy the structure?
Something like this:
...
pSpriteToReturn->vertices = (Vector2f*)malloc( sizeof(arraysize) );
memcpy(pSpriteToReturn->vertices, copyFromMe->vertices, sizeof(arraysize) );
...
Thank you in advance.