From 8726543676388742402
X-Google-Thread: f78e5,e2cfdf64f67c7793
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!peer01.cox.net!cox.net!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: sean@f4.ca (Sean Kelly)
Newsgroups: comp.std.c++
Subject: Re: Eliminating uninitialised variables
Date: Wed, 22 Sep 2004 17:16:26 GMT
Organization: http://groups.google.com
Lines: 60
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <c628f43d.0409212019.498d8c82@posting.google.com>
References: <memo.20040920104138.384A@brangdon.m>
NNTP-Posting-Host: news.news.demon.net
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: news.demon.co.uk 1095873391 29336 158.152.254.254 (22 Sep 2004 17:16:31 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Wed, 22 Sep 2004 17:16:31 +0000 (UTC)
X-Received-SPF: pass (mailbox2.ucsd.edu: domain of news@google.com designates 216.239.37.5 as permitted sender) receiver=mailbox2.ucsd.edu; client_ip=216.239.37.5; envelope-from=news@google.com;
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Spam-Checker-Version: SpamAssassin 2.64-mulga_r1 (2004-01-11) on 
	mulga.cs.mu.OZ.AU
X-Spam-Status: No, hits=-4.9 required=5.0 tests=BAYES_00 autolearn=ham 
	version=2.64-mulga_r1
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i8MHGQPB014647;
	Thu, 23 Sep 2004 03:16:26 +1000 (EST)
X-Path: comp-std-cpp-robomod!not-for-mail
X-NNTP-Posting-Date: Wed, 22 Sep 2004 04:19:17 +0000 (UTC)
X-Delivered-To: std-c++@ucar.edu
X-Spamscanner: mailbox2.ucsd.edu  (v1.5 Aug 25 2004 09:28:35, -2.3/5.0 3.0.0-rc1)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Newsgroups: comp.std.c++
X-MailScanner: PASSED (v1.2.8 70228 i8M4JIbk085865 mailbox2.ucsd.edu)
Xref: g2news1.google.com comp.std.c++:2831

brangdon@cix.co.uk (Dave Harris) wrote in message news:<memo.20040920104138.384A@brangdon.m>...
> In a comp.lang.c++.moderated thread about const correctness, the issue of 
> uninitialised variables came up. Accessing an uninitialised variable is a  
> source of undefined behaviour which requires constant programmer vigilance 
> to avoid. A mistake often leads to real bugs or crashes, which can be 
> irreproducible and so hard to track down.
> 
> Can we do something constructive towards fixing this? Has it been tried; 
> are there known problems? Here are my initial thoughts.

Much of the discussion centered initialization problems in classes. 
Specifically, missing an initialization among what may be a large
collection of contructors.  This problem will likely be reduced by
fowarding contructors, but is still worth addressing.  It seems the
simplest solution would be to adjust the wording of 12.6 to include
primitive types.

> We can't require the compiler to reject (at compile time) accesses of 
> uninitialised variables without breaking existing code, so I think we 
> would instead have to require all variables to be initialised -  
> zero-initialised as if they were statics. Since accessing an uninitialised 
> variable is currently undefined behaviour, a conforming program could not 
> tell the difference. I can think of two possible objections:
> 
> (1) A good compiler could use the undefined behaviour to detect accidents 
> at run-time, by secretly initialising to a trapping value.

My suggestion above would imply default initialization, but I suppose
this is another option.

> (2) Efficiency.

Static arrays are probably the strongest argument against default
initialization (since pointers could be considered initialized simply
by assigning nullptr), but perhaps a means to opt-out would be
sufficient?  And if so, where does this leave classes (which obviously
must be initialized)?  Is the lack of consistency acceptable?

> The efficiency issue we can probably handle by providing a way to request 
> uninitialised variables explicitly. Eg reusing the keyword "void":
>     int x; // x is 0.
>     int y = void; // y is uninitialised.
> 
> The important cases are arrays:
>     int array[1000] = void;

Default initialization across the board is a larger issue, though I
suppose it's worth considering for the sake of consistency.  Still, I
suspect there are fewer initialization oversights in standard
functions.


Sean

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



