From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 121756 invoked by alias); 18 Dec 2019 11:12:30 -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 121743 invoked by uid 89); 18 Dec 2019 11:12:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=H*Ad:D*126.com, Portable X-HELO: gate.crashing.org Received: from gate.crashing.org (HELO gate.crashing.org) (63.228.1.57) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Dec 2019 11:12:28 +0000 Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id xBIBCGK6021259; Wed, 18 Dec 2019 05:12:17 -0600 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id xBIBCF1X021256; Wed, 18 Dec 2019 05:12:15 -0600 Date: Wed, 18 Dec 2019 11:12:00 -0000 From: Segher Boessenkool To: David Brown Cc: Liu Hao , gcc-help@gcc.gnu.org Subject: Re: How to print pointer to function? Message-ID: <20191218111215.GV3152@gate.crashing.org> References: <20191212092116.GL2074@raven.inka.de> <1a9df1d1-df35-71e9-0f91-460e0728d62c@126.com> <20191212105305.GM2074@raven.inka.de> <20191217125648.GA458941@cventin.lip.ens-lyon.fr> <20191217132204.GK3152@gate.crashing.org> <20191217163001.GA486232@cventin.lip.ens-lyon.fr> <115d308d.1fc3.16f16dff26f.Coremail.lh_mouse@126.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-IsSubscribed: yes X-SW-Source: 2019-12/txt/msg00086.txt.bz2 Hi! On Wed, Dec 18, 2019 at 11:04:07AM +0100, David Brown wrote: > In practice, often you can write code that is portable > to any "reasonable" implementation, and accept that the code won't work > for other systems: > > #include > #if -INT_MAX == INT_MIN > #error Only two's complement signed integers supported by this code > #endif (And hopefully document it, and/or even do a test like you did here). > > 0. Conversion a value from `unsigned int` to `signed char` which doesn't > > fit in it yields an implementation-defined result. > > [C++14 now requires 2's complement, which is required by GCC.] > > IIRC it is C++20 that limits the signed integer representation to two's > complement. Yes, that is my understanding as well. > Note that it is entirely possible for an implementation to have two's > complement representation but /not/ use modulo to reduce a value to fit > into a smaller signed type. In particular, a compiler could choose to > raise a signal and halt with an error message (I don't know if any of > gcc's sanitizers do that). -fsanitize=signed-integer-overflow (part of -fsanitize=undefined, ubsan). [ lots of good stuff snipped ] > When you write some code, you first figure out what the code should do. > Then you can think about what range of systems it makes sense to > support. Portable coding is then usually fairly easy as long as the > target range is sensible. Yes, exactly. And the only good way to avoid undefined behaviour is simply to know what not to do. C is not a friendly language to beginners, in that sense. Segher