From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS17314 8.43.84.0/22 X-Spam-Status: No, score=-3.5 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_DNSWL_HI,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 050441F910 for ; Fri, 18 Nov 2022 02:47:31 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=cs.ucla.edu header.i=@cs.ucla.edu header.b="T/ZiUmxO"; dkim-atps=neutral Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D3371384F6D6 for ; Fri, 18 Nov 2022 02:47:29 +0000 (GMT) Received: from zimbra.cs.ucla.edu (zimbra.cs.ucla.edu [131.179.128.68]) by sourceware.org (Postfix) with ESMTPS id 0653E3852C68 for ; Fri, 18 Nov 2022 02:47:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0653E3852C68 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 588E2160040; Thu, 17 Nov 2022 18:47:16 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 494pvd_H6TNm; Thu, 17 Nov 2022 18:47:15 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 75B6A160043; Thu, 17 Nov 2022 18:47:15 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.9.2 zimbra.cs.ucla.edu 75B6A160043 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=78364E5A-2AF3-11ED-87FA-8298ECA2D365; t=1668739635; bh=jIfUT8RePCYPMQbd8+wWBAOKYK4w4ZlafHwReNK7aiE=; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type: Content-Transfer-Encoding; b=T/ZiUmxONthpf327SZ+Xcd+LN381n9IpWQWlsxCSepzoRF0onrx77i+Y7URUlXjy2 ucxsX5AVE6rwEslGUlrVb41aMqp8+Ts7y0B4wv3n3WcAd5vDx86tM74ffsq5ax3If/ DvqvevPzfpC/jd4x1I5+LvlvO28pMCUpcgd/oSig= X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id SKgUtMVkzhlj; Thu, 17 Nov 2022 18:47:15 -0800 (PST) Received: from [131.179.64.200] (Penguin.CS.UCLA.EDU [131.179.64.200]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 4B76D160040; Thu, 17 Nov 2022 18:47:15 -0800 (PST) Message-ID: <45e874b3-315f-a24b-0a4c-3c0d6d2955b3@cs.ucla.edu> Date: Thu, 17 Nov 2022 18:47:14 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: size_t vs long. Content-Language: en-US To: "Maciej W. Rozycki" Cc: Alejandro Colomar , A , libc-alpha@sourceware.org References: <380b196e-b78e-3b0e-7399-ee106b0e716c@gmail.com> <27229b18-673b-d038-9a4c-c32c50ca547c@cs.ucla.edu> From: Paul Eggert Organization: UCLA Computer Science Department In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" On 11/17/22 18:11, Maciej W. Rozycki wrote: >>> for (size_t i = 41; i < sizeof A / sizeof A[0]; --i) { >>> A[i] = something_nice; >>> } > ... what exactly is bogus with the first loop? > > AFAICT if index 41 is within the bounds of A, it fills elements [0..41] > with something_nice and otherwise it does nothing. Yes, and that's precisely what is bogus about it. Most people who read that code won't easily see that you're summarizing it correctly (assuming INT_MAX < SIZE_MAX). And if I saw that code in a real program, my first guess - and it most likely would be the correct guess - is that the *author* of the code didn't know what it does, it's so confusingly written. Certainly Alejandro was confused by that bogus loop, as his most recent email said the following: > For code that means exactly the same as > > for (size_t i = 41; i < nitems(A); --i) { > A[i] = something_nice; > } > > > we need: > > > for (size_t i = 0; i < nitems(A) && i <= 41; ++i) { > A[i] = something_nice; > } > > or > > for (idx_t i = 0; i < nitems(A) && i <= 41; ++i) { > A[i] = something_nice; > } > > or > > for (idx_t i = 41; i < nitems(A) && i > 0; --i) { > A[i] = something_nice; > } > > (always assuming SIZE_MAX > INT_MAX.) and if we call those four loops A, B, C and D, then Alejandro was incorrect about B and C because they are not equivalent to A. And although D is equivalent to A, D is still confusing and is still Bad Code. Code like this should be written more the way you said it. E.g.: idx_t nice_count = 42; if (nice_count <= nitems(A)) for (idx_t i = 0; i < nice_count; i++) A[i] = something_nice; This is much easier to understand than the other alternatives given so far, for reasons that I hope are obvious. And as a bonus, it doesn't assume INT_MAX < SIZE_MAX.