Thread: Is it possible to memcpy a std::vector?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Is it possible to memcpy a std::vector?

    I was playing around with using memcpy to copy a structure that contains a vector, but I could not get it to work. Im guessing its not possible as the memcpy probably just copies the pointer inside of std::vector. But, has anyone done it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why would you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Trying to memcpy a vector is in short undefined behavior. Use normal assignment or std::copy to copy it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In C++ you really should forget that memcpy() exists at all. For POD structure types, the default assignment operator and copy constructor already do the right thing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Well, as you probably know you cant always make all the decisions yourself.
    The program was already using memcpy to copy the data around and I didnt really want to rewrite it (its a biiiig program)

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It should be easy enough to re-factor the code to use the STL to copy the vector. Does the vector contain pointers to instances or instances of objects? Using memcpy() with any STL container is asking for trouble.
    Last edited by VirtualAce; 01-24-2010 at 01:20 PM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by h3ro View Post
    Well, as you probably know you cant always make all the decisions yourself.
    The program was already using memcpy to copy the data around and I didnt really want to rewrite it (its a biiiig program)
    I'm not sure I understand. This is some pre-existing program that uses memcpy() on structs which contain vectors? Then the program is incorrect, end of story, and needs rewriting anyway.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    It is a pre-existing program that uses memcpy() on some of its struct and I need to modify the struct, and adding a std::vector would be the easiest for me, but I could not get it to work

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by h3ro View Post
    It is a pre-existing program that uses memcpy() on some of its struct and I need to modify the struct, and adding a std::vector would be the easiest for me, but I could not get it to work
    How well designed is this program? Are there calls to memcpy() scattered all over the place, or are they concentrated in a small number of locations?

    I'd suggest a search-and-replace for all calls to memcpy(), and replace them with a call to CopyStruct() (choose a better name) -- inside that function, just use the assignment operator to perform the copy. Obviously, only replace the memcpy() calls which involve this struct.

    Whoever wrote the program in the first place must have had no understanding of C++ at all...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Trying to memcpy a vector is in short undefined behavior. Use normal assignment or std::copy to copy it.
    I don't think I would call it undefined behavior. It would just copy the vector's pointer to it's data, which is wrong and would cause all kinds of problems such as when the 2nd vector is destructed and it tries to delete the pointer a 2nd time...

    It sounds like the program was originally written in C, so updating it to use C++ types would require some careful refactoring anyways.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    It sounds like the program was originally written in C, so updating it to use C++ types would require some careful refactoring anyways.
    I'd go so far as to say, that if the structure is copied via memcpy(), then adding a non-POD member to it will probably break a lot more things than just the copying. I would find some other way to accomplish whatever the OP's goal is that does not involve altering this struct.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cpjust View Post
    I don't think I would call it undefined behavior. It would just copy the vector's pointer to it's data, which is wrong and would cause all kinds of problems such as when the 2nd vector is destructed and it tries to delete the pointer a 2nd time...
    Any attempt to use the "2nd vector" yields undefined behaviour. The "all kinds of problems" within its destruction are within that realm.

    The practical rule of thumb is that memcpy() should only be used to copy PoD (plain-old-data) types. C++ classes with an implemented (i.e. not the compiler supplied default) copy constructor or destructor are not PoD types. std::vector has a non-default copy constructor and destructor, so is not a PoD type.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing std::vector and optimisation
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 07-03-2008, 11:01 AM
  2. std::vector issues...
    By IndioDoido in forum C++ Programming
    Replies: 14
    Last Post: 11-25-2007, 10:01 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  5. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM