Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #66530

Re: trapping NaN to find uninitialised variables?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: trapping NaN to find uninitialised variables?
Date 2015-07-29 08:58 -0700
Organization None to speak of
Message-ID <lnio93lzve.fsf@kst-u.example.com> (permalink)
References <636d6775-affc-4f24-9394-f8b190c5174a@googlegroups.com> <lnmvymzzvl.fsf@kst-u.example.com> <d823fea1-f0df-47b3-888f-d908a3741be7@googlegroups.com>

Show all headers | View raw


pedro1492@lycos.com writes:
> On Thursday, July 23, 2015 at 10:56:39 PM UTC+8, Keith Thompson wrote:
>> pedro1492@lycos writes:
>> > Open64 has flag -trapuv and gcc has -finit-real=nan
>> > The claim is that variables are created NaN, so if your program does
>> > not initialise them before use, then it bites you on the arse.
>> [...]
>> 
>> What is the context for that claim?
>> 
>> C never guarantees that floating-point objects are initialized
>> to NaN.
>> 
> It is in the context of sloppy programming, that uses variables before they
> were initialised, so garbage in garbage out.
> The suggestion is that if initialised to NaN, then program would abend
> when some arithmetic is attempted. My query was that using a compiler flag
> is not enough, that a trap must be set as well.

That clarifies the *purpose* of initializing floating-point objects
to NaN.  I was asking about the claim that this is actually done.
As far as I can tell, it is not (though it would make sense to have
an option to do so).

As I wrote previously (and you snipped it), gcc's "-finit-real=nan"
applies only to Fortran, not to C.  And *static* floating-point
objects are initialized to 0.0 in the absence of an explicit
initializer.

At least in IEEE, there are "quiet NaNs" and "signalling NaNs".
Initializing floating-point objects to a signalling NaN would,
as I understand it, cause a trap on any attempt to access their
value without any further action (though that might depend on how
the system is configured).  (The optional macro NAN in <math.h>
expands to a quiet NaN; I don't think there's any support for
signalling NaNs in the standard.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

trapping NaN to find uninitialised variables? pedro1492@lycos.com - 2015-07-23 01:42 -0700
  Re: trapping NaN to find uninitialised variables? Bartc <bc@freeuk.com> - 2015-07-23 10:35 +0100
  Re: trapping NaN to find uninitialised variables? Robert Wessel <robertwessel2@yahoo.com> - 2015-07-23 04:51 -0500
  Re: trapping NaN to find uninitialised variables? raltbos@xs4all.nl (Richard Bos) - 2015-07-23 11:03 +0000
  Re: trapping NaN to find uninitialised variables? Keith Thompson <kst-u@mib.org> - 2015-07-23 07:56 -0700
    Re: trapping NaN to find uninitialised variables? pedro1492@lycos.com - 2015-07-29 05:39 -0700
      Re: trapping NaN to find uninitialised variables? Keith Thompson <kst-u@mib.org> - 2015-07-29 08:58 -0700
  Re: trapping NaN to find uninitialised variables? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-23 10:39 -0700

csiph-web