7

構造体へのポインタがあり、構造体のすべてのメモリコンテンツをコピーするメソッドを実装する必要があります。一般的に言えば、構造のディープコピーを実行する必要があります。

構造は次のとおりです。

typedef struct { 
    Size2f spriteSize;

    Vertex2f *vertices;

    GLubyte *vertex_indices;
} tSprite;

そして、これが私が実装した、構造をコピーする必要のあるメソッドです。

tSprite* copySprite(const tSprite *copyFromMe)
{

    tSprite *pSpriteToReturn = (tSprite*)malloc( sizeof(*copyFromMe) );

    memcpy(pSpriteToReturn, copyFromMe, sizeof(*copyFromMe) );

    return pSpriteToReturn;
}

問題は、配列「vertices」と「vertex_indices」が正しくコピーされるかどうかわからないことです。この方法で何がコピーされますか?アレイのアドレスまたはアレイ自体?

構造をコピーした後に配列をコピーする必要がありますか?それとも、構造をコピーするだけで十分ですか?

このようなもの:

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

前もって感謝します。

4

7 に答える 7

8

As a rule of thumb, don’t ever use memcpy in C++ in normal code (it might crop up in very low-level code, e.g. in allocators)1). Instead, create a suitable copy constructor and overload operator = (the assignment operator) to match it (and a destructor – rule of three: “if you implement either of copy constructor, operator = and destructor, you must implement all three).

If you do not implement your own versions of the copy constructor an the assignment operator, C++ will create default versions for you. These versions will implement a shallow copy (much like what memcpy would do), i.e. in your case the array contents would not be copied – only the pointers.


1) Incidentally, the same goes for malloc and free. Don’t use them, instead use new/new[] and delete/delete[].

于 2010-01-21T19:51:58.847 に答える
3

This partially depends on your requirements. If you don't copy the arrays, both structures will be pointing to the same array, which may or may not be a problem.

于 2010-01-21T19:49:59.367 に答える
3

Your scheme is going to copy the addresses of the arrays. The "copy" tSprite returned is going to have pointers to the same data (in memory) as the passed in one.

If you want a true deep-copy, you'll need to copy the arrays (and any members of their elements) manually.

于 2010-01-21T19:50:48.697 に答える
2

あなたが書いているのがC++の場合、C++には理由があることを忘れないでnewくださいdelete。質問自体に関しては、ポインタをコピーするか、構造自体をコピーするかによって異なります。後者の場合は、それらもコピーする必要があります!

于 2010-01-21T19:52:45.187 に答える
1

プレーンCで作業している場合でも、これはコピーする正しい方法ではありません。

他の回答で指摘されているように、2つ(またはそれ以上)の構造体インスタンスが同じインスタンスを指していることにVertext2なりGLubyteますが、これはお勧めできません。

これは、誰がメモリ割り当てを解放するかなどの問題につながりますVertext2 GLubyte

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

はい、これは正しい方法です

于 2010-01-21T19:57:34.030 に答える
1

The pointers themselves will be copied, but that means that both "from" and "to" will be the same in the two sprites. You'll also need to manually allocate and copy the things pointed to by the pointers, but that implies that you also need to know how big the arrays are that are referenced by the pointers.

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

Another note would be that if your sprites always have a fixed number of vertices and vert indices, you can make those arrays inside the sprite rather than pointers. If you did this, then they would be copied correctly with both the memcpy method and the assignment that I mention in the above paragraph.

于 2010-01-21T19:58:29.147 に答える
1

in C++ new and delete allocate on 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[]
于 2010-01-21T20:03:13.173 に答える