Date: Sun, 7 Nov 1999 17:28:53 +0200 (IST) From: Eli Zaretskii X-Sender: eliz AT is To: Dave Dribin cc: djgpp AT delorie DOT com Subject: Re: Allegro 3.12: page fault in generate_optimized_palette() In-Reply-To: <800gho$aut$1@eve.enteract.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk 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 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.