Sign in to follow this  
littlekid

memcpy and std::wstring

Recommended Posts

littlekid    229
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 this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by littlekid
Is 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 this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by ToohrVyk
Quote:
Original post by littlekid
Is 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 this post


    Link to post
    Share on other sites
    littlekid    229
    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 this post


    Link to post
    Share on other sites
    Julian90    736
    Quote:
    That was why I wanted to use memcy :D
    Is there a way to easily copy the array rather than iterate it?


    You where already told, std::copy.
    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 this post


    Link to post
    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 this post


    Link to post
    Share on other sites
    ToohrVyk    1595
    Quote:
    Original post by Emmanuel Deloget
    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.


    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 this post


    Link to post
    Share on other sites
    Quote:
    Original post by ToohrVyk
    Quote:
    Original post by Emmanuel Deloget
    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.


    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).

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this