Is it possible to memcpy a std::vector?

This is a discussion on Is it possible to memcpy a std::vector? within the C++ Programming forums, part of the General Programming Boards category; I was playing around with using memcpy to copy a structure that contains a vector, but I could not get ...

  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
    21,648
    Why would you want to do this?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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 12:20 PM.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,261
    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%.

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, 09: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, 10:35 AM
  5. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21