Solved

memmove

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use switch statements in the C programming language.

773 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