Solved

memmove

Posted on 2003-11-25
3
543 Views
Last Modified: 2010-04-15
How memmove different from memcpy interms of implimentation ,like man pages mentions that it takes care memory overlap,can anybody throw some sample code.

Appericiate u r help in advance
Chiku
0
Comment
Question by:chikucoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9816646
Hi chikucoder,

a memcpy implementation will not take into consideration the relative positions of source and destination ... In most of the optimized implementation, they check if they have more than a (remaining) word to copy ... if yes, they copy a word else they copy the bytes ...

memove on the other hand takes into account the relative positions of the source and destination and if it is copying the string forward or backward or on overlapping areas ... here is the glibc implementation of memmove ... while it is not very descriptive, it is intutive enough to give an idea...

memmove (a1, a2, len)
     a1const void *a1;
     a2const void *a2;
     size_t len;
{
  unsigned long int dstp = (long int) dest;
  unsigned long int srcp = (long int) src;

  /* This test makes the forward copying code be used whenever possible.
     Reduces the working set.  */
  if (dstp - srcp >= len)      /* *Unsigned* compare!  */
    {
      /* Copy from the beginning to the end.  */

      /* If there not too few bytes to copy, use word copy.  */
      if (len >= OP_T_THRES)
      {
        /* Copy just a few bytes to make DSTP aligned.  */
        len -= (-dstp) % OPSIZ;
        BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);

        /* Copy whole pages from SRCP to DSTP by virtual address
           manipulation, as much as possible.  */

        PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);

        /* Copy from SRCP to DSTP taking advantage of the known
           alignment of DSTP.  Number of bytes remaining is put
           in the third argument, i.e. in LEN.  This number may
           vary from machine to machine.  */

        WORD_COPY_FWD (dstp, srcp, len, len);

        /* Fall out and copy the tail.  */
      }

      /* There are just a few bytes to copy.  Use byte memory operations.  */
      BYTE_COPY_FWD (dstp, srcp, len);
    }
  else
    {
      /* Copy from the end to the beginning.  */
      srcp += len;
      dstp += len;

      /* If there not too few bytes to copy, use word copy.  */
      if (len >= OP_T_THRES)
      {
        /* Copy just a few bytes to make DSTP aligned.  */
        len -= dstp % OPSIZ;
        BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);

        /* Copy from SRCP to DSTP taking advantage of the known
           alignment of DSTP.  Number of bytes remaining is put
           in the third argument, i.e. in LEN.  This number may
           vary from machine to machine.  */

        WORD_COPY_BWD (dstp, srcp, len, len);

        /* Fall out and copy the tail.  */
      }

      /* There are just a few bytes to copy.  Use byte memory operations.  */
      BYTE_COPY_BWD (dstp, srcp, len);
    }

  RETURN (dest);
}

Cheers!
Sunny:o)
0
 
LVL 1

Author Comment

by:chikucoder
ID: 9816663
My god !!!!!!!!!!!!!!!
Herculean effort,Thanks indeed.Don't mind sunny i guess u work for experts-exchange :)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9816664
>Don't mind sunny i guess u work for experts-exchange :)
LOL, you know whom I work for ;o) ... just dont tell my bosses
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question