Thread: memcpy() faster?

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Quote Originally Posted by Yasir_Malik
    Code:
    a.a = *(int *) buf;
    a.b = *(int *) (buf + 4);
    a.c = *(int *) (buf + 8);
    Why all that when
    Code:
    a = *(A*)buf;
    does the same thing without worrying about the shape of the structure? But as mentioned by someone else, memcpy is the way to go because of possible alignment issues.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by codie
    Why all that when
    Code:
    a = *(A*)buf;
    does the same thing without worrying about the shape of the structure? But as mentioned by someone else, memcpy is the way to go because of possible alignment issues.
    probably depending on compiler implementation, your suggestion is probably implemented as memcpy() anyway.

    I wrote a tiny test program with VC++ 2005 Pro did a release build, and had it produce an assembly listing. It looks like does not do any copying at all, just replaces a pointer to the struct object with a pointer to buf.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > It looks like does not do any copying at all, just replaces a pointer to the struct object with a pointer to buf
    Yes, beware of optimisers being extremely effective in working out what really small programs do.
    It probably decided that buff was never used after the assignment, so the easy thing to do was just re-use buff for it's new purpose.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Well back in the days of 8080 we would lose precious cycles for the rep prefix so prefixes had to be watched in general, but that's obviously no longer a problem.

    MMX cannot copy from memory to memory so I don't see how this would speed anything up.
    Neither can a normal move, but with mmx your working with x86 quadwords instead of double words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disagreement about memcpy
    By ch4 in forum C Programming
    Replies: 9
    Last Post: 05-28-2009, 10:12 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM