Mail Archives: djgpp/1999/02/06/21:18:27
On Sat, 6 Feb 1999, George Lugovoy wrote:
> I has a program:
>
> //////////////////////////////////////////////////////////////////////
> #include <stdlib.h>
>
> int main( void )
> {
> char buffer[1024];
> memcpy( buffer, NULL, sizeof( buffer ) );
> return 0;
> }
> //////////////////////////////////////////////////////////////////////
>
> Under DOS, Unix that program is crashed, BUT
> under Windows that program won't crash.
> What is it ? Is that bug ?
The program has undefined effect. It's trying to read 1024 bytes of memory
starting at the address NULL, which is an invalid address. Such bugs
typically have different effects on different systems. As I understand it,
the system is allowed (in the definition of the C language) to respond
however it likes. However, I think that running the above program through
the debugger GDB will reveal the error, on any system.
Another problem is that memcpy() is not declared in <stdlib.h>. memcpy()
is declared in <string.h>. This could lead to trouble on some systems, due
to accidental casting of pointers to integers.
Although NULL is defined in DJGPP's header <stdlib.h>, Kernighan & Ritchie
(2nd edition) says it is defined in <stdio.h>. So, to be portable, you
should probably include both <string.h> and <stdio.h>, in addition to
fixing the main bug in the program.
Daniel Barker,
Institute of Cell and Molecular Biology,
Swann Building,
King's Buildings,
Mayfield Road,
Edinburgh
EH9 3JR
UK
- Raw text -