Public Group

# memcpy and std::wstring

This topic is 4000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I can't seems to do a memcpy for a wstring. Everytime I do this, std::wstring Src = L"Cube bone"; std::wstring Dest; memcpy(&Dest, &Src, sixeof(std::wstring)); It produces a error that the heap is corrupted and opens the xtree file. Is there anything wrong I am doing.

##### Share on other sites
Ehm, why don't you just do this:

std::wstring Src = L"Cube bone";
std::wstring Dest;
Dest=Src;

Gero

##### Share on other sites
Quote:
 Original post by littlekidIs there anything wrong I am doing.

You are doing three things wrong.
1. You're using memcpy to perform a byte-wise copy of a non-POD type. This was already wrong in C (where memcpy is used only for copying POD types), and is even worse in C++, where it breaks encapsulation by relying on the internal representation of the string.
2. You're using memcpy in C++. It is in all ways inferior to std::copy, which will act as memcpy for simple types (the ones compatible with memcpy) and will also act sanely for complex types (the ones not compatible with memcpy).
3. You're not using the assignment operator to assign one variable to another. In modern C++, assigning a to b is done by writing b = a, no weird hacks (such as byte-wise copy) are necessary to get this right.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by littlekidIs there anything wrong I am doing.

You are doing three things wrong.
1. You're using memcpy to perform a byte-wise copy of a non-POD type. This was already wrong in C (where memcpy is used only for copying POD types), and is even worse in C++, where it breaks encapsulation by relying on the internal representation of the string.
2. You're using memcpy in C++. It is in all ways inferior to std::copy, which will act as memcpy for simple types (the ones compatible with memcpy) and will also act sanely for complex types (the ones not compatible with memcpy).
3. You're not using the assignment operator to assign one variable to another. In modern C++, assigning a to b is done by writing b = a, no weird hacks (such as byte-wise copy) are necessary to get this right.

• You are trying to use the sizeof() operator to get the length of a string, which (a) wouldn't be appropriate for a wide string no matter what the representation (since each character typically takes two bytes in a wide string), (b) was never correct for C string literals (you could get the size of a *buffer containing the string*, or the size of a *pointer to the string data*, but never the length of the string, in this way), and (c) is definitely not correct for class types.
• You have it spelled as "sixeof". :) (I assume that's only a typo in the post, though, since you apparently managed to compile.)

##### Share on other sites
Thanks alot guys.

Well i actuall used memcpy, because I was copying a whole array structures.

I have:

struct BONE{    std::wstring Name;    D3DXQUATERNION Orientation;    D3DXVECTOR3 Position;};and instead of doing:for (int iBone=0; iBone<NumBones; iBone++){    NewBone[iBone] = OldBone[iBone];}I was wondering if i could do this:memcpy(NewBone, OldBone, sizeof(BONE) * NumBones);

That was why I wanted to use memcy :D
Is there a way to easily copy the array rather than iterate it?

p.s what does POD stand for?

Thanks

##### Share on other sites
Quote:
 That was why I wanted to use memcy :DIs there a way to easily copy the array rather than iterate it?

If NewBone and OldBone are std::vector's (which they should be)
std::copy(OldBone.begin(), OldBone.end(), NewBone.begin());
And if they are arrays or pointers acting as arrays
std::copy(OldBone, OldBone + NumBones, NewBone);

Quote:
 p.s what does POD stand for?

Plain Old Data, any type with only POD members and only the default copy constructor assignment operator and default constructor or any fundamental type.

##### Share on other sites
Quote:
Original post by Julian90
Quote:
 p.s what does POD stand for?

Plain Old Data, any type with only POD members and only the default implicit copy constructor, default implicit assignment operator and default implicit constructor or any fundamental type.

Corrected a bit, because I'm a bit bored by the code I'm writing right now. Sorry.

##### Share on other sites
Quote:
 Original post by Emmanuel DelogetPlain Old Data, any type with only POD members and only the default implicit copy constructor, default implicit assignment operator and default implicit constructor or any fundamental type.

It's a little worse than that. The code below, though it satisfies the definition above, is not a POD:
class foo { virtual void bar() const {} };

I'd define a POD type as any type which can be placed in an union [smile]

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by Emmanuel DelogetPlain Old Data, any type with only POD members and only the default implicit copy constructor, default implicit assignment operator and default implicit constructor or any fundamental type.

It's a little worse than that. The code below, though it satisfies the definition above, is not a POD:
class foo { virtual void bar() const {} };

I'd define a POD type as any type which can be placed in an union [smile]

Yep. I must modify again the definition above, for the sake of completeness.

From the standard:
Quote:
 An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions.

And:
Quote:
 A POD-struct is an aggregate class that has no non-static data members of type pointer to member, non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignement operator and no user-defined destructor.

So that left us with something along the line of: data members are either public non-static POD-types variables or private/protected static variables. This definition doesn't says that a POD type shall not have any non-virtual member functions (although it shall not have any virtual member function, user-defined constructor, destructor or assignment operator).

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631724
• Total Posts
3001904
×