home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: Memory functions - memcpy & memmove
- Message-ID: <BxB88A.1Au@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <BxArt0.9pE@murphy.com>
- Date: Fri, 6 Nov 1992 19:45:45 GMT
- Lines: 58
-
- gregb@murphy.com (Greg Banschbach (Unigroup of New York)) writes:
- :
- : Dear fellow programmers,
- :
- : I am in the process of porting a program from MS-DOS to
- : UNIX. The program is a database program. It calls "memmove"
- : which is not available under Unix Sys5 rel 2. I substituted
- : memcpy, in order to get it to compile. Now I discovered that
- : what appears to be happening is that I am getting DUPLICATE
- : records in the INDEX file - probably when the b-tree is moving
- : nodes around.
- :
-
- Greg,
- memcpy does not account for overlapping regions, it just does a simple
- move of x bytes from pointer 1 to pointer 2, if they overlap, too bad.
- memmove handles that, but so can you.
-
- write your own memmove (different name of course).
-
- lets say you call it moveit(char * dest, char *source, int size)
-
- then the code will look like this
-
- {
- char *temp;
-
- temp = (char *) malloc(size);
-
- memcpy(temp, source, size);
- memcpy(dest, temp, size);
-
- free (temp);
- }
-
- If you don't like using malloc, there is another way.
-
- Test the value of the pointers to see which is greater.
-
- If SOURCE is greater then you can copy from the beginning of source
- to the beginning of dest 1 char at a time with no problem.
-
- for(;size > 0; size --)
- *dest++ = *source++;
-
- If DEST is greater you need to copy from source to dest 1 char at
- a time starting at the ends of the areas.
-
- for(size --; size >= 0; size --)
- dest[size] = source[size];
-
-
- This should do it !!
-
- Dave Fuller
- dfuller@portal.hq.videocart.com
-
-
-