Inexplicable memcpy bug in gcc
Jeffrey A Law
law@cygnus.com
Tue Aug 31 22:45:00 GMT 1999
In message < 7qainu$dr3$1@nnrp1.deja.com >you write:
> I have encountered an incredible and inexplicable
> bug in gcc version egcs 1.1.2. I have whittled the
> problem down to the simplest possible source code:
>
> **************** start of code *******************
> // compiled by typing "gcc main.cpp -o main"
> #include <stdio.h>
> #include <string>
>
> // this one crashes on memcpy with segmentation
> fault
> void main()
> {
> char* buffer_ptr = "abcdefghijklmnop";
>
> long * outBuff = (long *)malloc(4);
> memcpy(buffer_ptr, outBuff, 1);
Please read the gcc manual. The string you assigned to buffer_ptr is a
readonly constant and thus may be placed in readonly memory. Attempts to
write to readonly memory are likely to fault on modern systems.
Your program is buggy, not the compiler.
jeff
More information about the Gcc-bugs
mailing list