www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/28/02:59:53

Date: Tue, 28 May 1996 09:48:12 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Rikard Thunberg <thunberg AT algonet DOT se>
Cc: djgpp AT delorie DOT com
Subject: Re: printf("%s",0) - Segmentation fault
In-Reply-To: <31A987B6.F1@algonet.se>
Message-Id: <Pine.SUN.3.91.960528094336.11160B-100000@is>
Mime-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019