home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16092 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.8 KB  |  70 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!portal!dfuller
  3. From: dfuller@portal.hq.videocart.com (Dave Fuller)
  4. Subject: Re: Memory functions  -  memcpy & memmove
  5. Message-ID: <BxB88A.1Au@portal.hq.videocart.com>
  6. Organization: VideOcart Inc.
  7. X-Newsreader: Tin 1.1 PL3
  8. References: <BxArt0.9pE@murphy.com>
  9. Date: Fri, 6 Nov 1992 19:45:45 GMT
  10. Lines: 58
  11.  
  12. gregb@murphy.com (Greg Banschbach (Unigroup of New York)) writes:
  13. :     Dear fellow programmers,
  14. :     
  15. :         I am in the process of porting a program from MS-DOS to
  16. :     UNIX.   The program is a database program.   It calls "memmove"
  17. :     which is not available under  Unix Sys5 rel 2.   I substituted
  18. :     memcpy,  in order to get it to compile.   Now I discovered that
  19. :     what appears to be happening is that I am getting DUPLICATE 
  20. :     records in the INDEX file - probably when the b-tree is moving
  21. :     nodes around.  
  22. :     
  23.  
  24.   Greg,
  25.  memcpy does not account for overlapping regions, it just does a simple
  26. move of x bytes from pointer 1 to pointer 2, if they overlap, too bad.
  27. memmove handles that, but so can you.
  28.  
  29. write your own memmove (different name of course).
  30.  
  31. lets say you call it  moveit(char * dest, char *source, int size)
  32.  
  33. then the code will look like this
  34.  
  35. {
  36.   char *temp;
  37.  
  38.   temp = (char *) malloc(size);
  39.  
  40.   memcpy(temp, source, size);
  41.   memcpy(dest, temp, size);
  42.  
  43.   free (temp);
  44. }
  45.  
  46. If you don't like using malloc, there is another way.
  47.  
  48. Test the value of the pointers to see which is greater.
  49.  
  50. If SOURCE is greater then you can copy from the beginning of source
  51. to the beginning of dest 1 char at a time with no problem.
  52.  
  53. for(;size > 0; size --)
  54.   *dest++ = *source++;
  55.  
  56. If DEST is greater you need to copy from source to dest 1 char at 
  57. a time starting at the ends of the areas.
  58.  
  59. for(size --; size >= 0; size --)
  60.   dest[size] = source[size];
  61.  
  62.  
  63. This should do it !!
  64.  
  65. Dave Fuller
  66. dfuller@portal.hq.videocart.com
  67.  
  68.     
  69.