Mail Archives: djgpp/1996/05/28/02:59:53
On Mon, 27 May 1996, Rikard Thunberg wrote:
> The following line will produce a "Segmentation fault" -error on some platforms/compilers:
> printf("%s",0);
>
> However, some platforms/compilers will produce a string like "(Null)" or similar.
You are obviously dereferencing a null pointer here (when `printf'
accesses the ``characters'' pointed to by the 0). Any decent
protected-mode environment will blow your program off the water in such
cases. If `printf' explicitly checks its arguments before accessing
them, it can generate "(Null)", but nothing in the ANSI C stanadrd
requires it to do so (AFAIK).
> Is it possible to tell the compiler what to do in this situation?
This has nothing to do with the compiler, but with the way the library
functions are implemented. The error must be detected at runtime, not at
compile time (because the arguments to `printf' can be variables rather
than constants).
- Raw text -