Date: Wed, 9 Dec 1998 16:52:15 +0200 (IST) From: Eli Zaretskii X-Sender: eliz AT is To: Freddie Schwenke cc: djgpp AT delorie DOT com Subject: Re: memcpy() vs memmove() In-Reply-To: <74lrv4$k9i$1@hermes.is.co.za> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com On Wed, 9 Dec 1998, Freddie Schwenke wrote: > I'm quite aware that there is a differrence between the two functions. I > actually want to abuse the memcpy() function to replicate one ore more bytes > throughout a string. You CANNOT use `memcpy' to copy overlapping regions. You just can't. The reason is that `memcpy' uses an algorithm that fails to do the right thing when the source and the target regions overlap. You must use `memmove' in that case. > The problem is that memcpy() only works while the length is less that > 16. WHY??? This is just one of the manifestations of the failure I was talking about. If you *really* want to know the exact reason, look at the assembly code of `memcpy'. But I don't think the exact reason is relevant, or even interesting. Just don't use `memcpy' when the regions overlap, it doesn't work in these cases. > Is this a C thing or a DJGPP thing? In MS Visual C++ apparently there is no > difference betwee the two functions!!! It depends on how the library was written, obviously. In addition, GCC sometimes produces inline assembly code instead of generating a call to `memcpy' from the library, so the compiler also matters. But the ANSI standard explicitly says that `memcpy' behavior for overlapping regions is ``undefined'' (read: anything can happen). So I guess you could say it's ``a C thing''.