From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29556 invoked by alias); 1 Aug 2014 08:53:52 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 29521 invoked by uid 89); 1 Aug 2014 08:53:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 X-HELO: mailfilter05.hatteland.com Received: from mailfilter05.hatteland.com (HELO mailfilter05.hatteland.com) (213.162.250.24) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 01 Aug 2014 08:53:49 +0000 Received: from mailfilter05.hatteland.com (localhost [127.0.0.1]) by mailfilter05.hatteland.com (Postfix) with ESMTP id 2731CF1A49; Fri, 1 Aug 2014 10:53:44 +0200 (CEST) Received: from mailfilter05.hatteland.com (localhost [127.0.0.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mailfilter05.hatteland.com (Postfix) with ESMTPS id EC659F1A5B; Fri, 1 Aug 2014 10:53:41 +0200 (CEST) Received: from JHSVMHUB01.netsentral.no (unknown [172.21.1.101]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mailfilter05.hatteland.com (Postfix) with ESMTPS id E9186F1A49; Fri, 1 Aug 2014 10:53:41 +0200 (CEST) Received: from [192.168.0.65] (172.21.20.11) by e2010.hatteland.com (172.21.1.25) with Microsoft SMTP Server (TLS) id 14.3.181.6; Fri, 1 Aug 2014 10:53:42 +0200 Message-ID: <53DB5595.1030101@westcontrol.com> Date: Fri, 01 Aug 2014 08:53:00 -0000 From: David Brown User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Ricardo Telichevesky , Subject: Re: Compiler warnings, overflow References: <53DA7643.2000004@teli.org> In-Reply-To: <53DA7643.2000004@teli.org> Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-SW-Source: 2014-08/txt/msg00001.txt.bz2 On 31/07/14 19:00, Ricardo Telichevesky wrote: > Hi, hope this is the right list. > > Here is my code and output, at the bottom of the e-mail. y is "correct", > w and z obviously have problems - multiplying two 32-bit integers > "hoping" the result would be correct assigning to 64-bit - I guess it is > the same problem as double oneThird= 1/3; the result being zero, and > not 0.3333. > > I was wondering if there is any strict warning that would flag the w and > z assignments below, or the 1/3 above - the whole right hand side is > evaluated as a 32-bit integer number, and assigned to a 64-bit integer > or double. Not advocating this should be a default, but turning it on > would help me detect some flaws in the code. Took me hours to catch a > similar bug in my code, trying to solve a sparse system that has > hundreds of millions of variables... > > Thanks! > Ricardo > > laplace utils % cat ovr.c > #include > int main() > { > > unsigned int x = 1015625426; > unsigned int t = sizeof(double); > > size_t y = x * sizeof(double); > size_t w = x << 3; > size_t z = x * t; > > printf("y= %zd w = %zd z = %zd\n", y, w, z); > } > laplace utils % gcc -Wall -o ovr ovr.c > laplace utils % ovr > y= 8125003408 w = 3830036112 z = 3830036112 > > Hi, As others have said, it's not easy to warn about this sort of thing since it is perfectly valid C - and many programs rely on the overflow behaviour of unsigned integers. But as a stylistic point, you should probably avoid using types like "unsigned int" and "size_t" when you are concerned about integer sizes - it is far safer, clearer, and more portable to use the size-specific types in such as "uint32_t" and "uint64_t". Of course, you might want to use typedefs to make things even clearer, or to allow you to easily change the sizes at a later date. But start from the types. Another point is to remember to enable optimisation. It won't help in this case, but some warnings work better when optimisation (at least -O1) is enabled. And of course your code will run far faster. David