Solved

memmove

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

758 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

22 Experts available now in Live!

Get 1:1 Help Now