From -8473022370563029167
X-Google-Thread: f78e5,3c316a202087bfac
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!nntp.theplanet.net!inewsm1.nntp.theplanet.net!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: petebecker@acm.org (Pete Becker)
Newsgroups: comp.std.c++
Subject: Re: Defining undefined, etc., behavior
Date: Tue, 18 Apr 2006 14:53:31 GMT
Organization: Roundhouse Consulting, Ltd.
Lines: 87
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <kPqdnVYgROPWVdnZRVn-jg@giganews.com>
References: <123m587c3abqge4@corp.supernews.com> <0dmdnUZuzOJIWqLZ4p2dnA@giganews.com> <e1p3fi$bj2$1@emma.aioe.org> <d96Ix9gHeDQEFwb2@robinton.demon.co.uk> <e1q8q9$1se$1@emma.aioe.org> <4whRCPfpHhQEFwfC@robinton.demon.co.uk> <9uSdnZJJpJGsBd_ZnZ2dnUVZ_v2dnZ2d@giganews.com> <nhQGWfCtM3QEFwcM@robinton.demon.co.uk>
NNTP-Posting-Host: news.news.demon.net
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.demon.co.uk 1145372015 20703 158.152.254.254 (18 Apr 2006 14:53:35 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Tue, 18 Apr 2006 14:53:35 +0000 (UTC)
X-Original-To: std-c++@mailman.ucar.edu
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Accept-Language: en-us, en
X-Virus-Scanned: amavisd-new at cs.mu.OZ.AU
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Path: comp-std-cpp-robomod!not-for-mail
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id k3IErVPI013181;
	Wed, 19 Apr 2006 00:53:31 +1000 (EST)
X-NNTP-Posting-Date: Tue, 18 Apr 2006 06:06:51 -0500
X-Delivered-To: std-c++@ucar.edu
X-Postfilter: 1.3.32
X-Authentication-Warning: serv2.gc.dca.giganews.com: news set sender to poster@giganews.com using -f
X-Newsgroups: comp.std.c++
Xref: g2news1.google.com comp.std.c++:1589

Francis Glassborow wrote:

> In article <9uSdnZJJpJGsBd_ZnZ2dnUVZ_v2dnZ2d@giganews.com>, Pete Becker
> <petebecker@acm.org> writes
> 
>>>However every time I do arithmetic I
>>>have to go through the same thought and error trapping.
>>
>>No, you don't. Once you've validated the input you don't have to keep
>>rechecking it.
> 
> 
> But input includes arguments and values returned from functions. It is
> actually relatively rare that none of the data used in an arithmetic
> expression is from some form of external (to the current function)
> input.
> 

I guess I underestimated the possibilities of misinterpreting "input." I 
meant input to the program, not whatever data you happen to be working 
with at the time.

> 
>>>IOWs the error
>>>trapping becomes part of the normal code.
>>
>>Input validation ought to be part of the normal code. Anything else is too
>>little, too late.
> 
> 
> I see so I should never write:
> (given the specifications of foo() and bar() are that they return a
> positive int)
> 
> int func(int val){
>   int i = foo(val) + bar(val);
>   if(i < 0) throw error;
>   // rest of function
> };
> 

There's no program here, just a fragment. Where did val come from, and 
what are the full specifications for foo and bar? Validating input means 
doing the work of tracing data flows. That's part of what's referred to 
in the literature as "low level design."

> Programs should always avoid undefined behaviour, but other forms of
> error can often be dealt with internally but undefined behaviour makes
> that too late.
> 

Undefined behavior combined with lack of data analysis makes error 
checking impossible. But it's not the undefined behavior that causes the 
problem. It's the lack of data analysis.

To go back to your original example:

#include <iostream>

int main(){
   int i(2);
   int j(0);
   std::cout << "Please input a number: ";
   std::cin >> j;
   if(std::cin.good(){
      j *= 2;
   }
   else {
     std::cout << "That was not accepted as a number. \n";
   }
}

Change j *= 2 to j *= INT_MAX. Does your analysis change? Now the only 
valid inputs to your program are -1, 0, and 1. Are you still not going 
to check that you got valid input?

-- 

Pete Becker
Roundhouse Consulting, Ltd.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]



