Mail Archives: djgpp/1999/11/07/12:41:05
On 6 Nov 1999, Dave Dribin wrote:
> As you can see (max - j) ends up being negative and causing havoc on
> the memcpy(). Is this a known problem? Am I using the function
> wrong?
Yes and yes. memcpy has the following prototype (see <string.h> and
the documentation of memcpy):
void * memcpy(void *_dest, const void *_src, size_t _n);
The last argument is type size_t, which is an unisigned integral
type. So your negative value is interpreted as a very large positive
value, and causes your program to crash because you are writing
outside the limits of the array common[].
In other words, your code has a bug.
In addition, please note that memcpy is not supposed to be used for
copying overlapping regions, and your code does precisely that. Use
memmove if the source and destination overlap.
- Raw text -