[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

memmove

Posted on 2003-11-25
3
Medium Priority
?
545 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
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 how to create, access, and change arrays in the C programming language.

650 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