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?
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?
Why would you want to do this?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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); //}
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)
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.
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); //}
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
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); //}
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.