Solved

memmove

Posted on 2003-11-25
3
538 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
  • 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

Make managing Office 365 email signatures a breeze

Are you using Office 365? Having trouble trying to set up email signatures for your users? Getting stressed out managing multiple signatures? Need an easier way to manage? We have a solution for you, try the most-user friendly and powerful signature management tool on the market.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now