copying an object using memcpy

This is a discussion on copying an object using memcpy within the C++ Programming forums, part of the General Programming Boards category; hi all, I have a piece of code which suppose to reset the data members of a structure ( there ...

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    copying an object using memcpy

    hi all,

    I have a piece of code which suppose to reset the data members of a structure ( there are around 15 member variables ), this "clear" function gets called frequently and I want to make it run as fast as possible. Right now, I just use "assignment" operator. Can I use memcpy to copy over an initialized object over the object that I want to initialize ?. Will copying objects like this have any bad effects ?

    Thank you
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This might work, if the struct contains only simple types. It will have ill effects, though, if the struct contains pointers or something that contains pointers (e.g std::string).

    If the structure needs to be reset at the beginning of a loop, may-be you can construct it there. (May-be the compiler generates some extra good code, if it sees that an object with unchanging arguments is constructed over and over again.)

    By the way, it seems that assignment is faster (for a struct of 3 ints and a char).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  2. Deleting object after list removal (C++ visual studio)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-20-2005, 07:06 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM

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