From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 2F3AD3858D1E for ; Tue, 25 Apr 2023 19:21:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F3AD3858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=jguk.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jguk.org Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-304935cc79bso780123f8f.2 for ; Tue, 25 Apr 2023 12:21:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; t=1682450487; x=1685042487; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=dk0+KrWcmsCzCU9lXnwYuCuV6Pn3DqiOUPlx74ityKM=; b=DIhLYeB+oZN8xkYYbPD6uOmGeuojxQv/I8n0oyJkBWrTOBrZirJler+hUeZJo+vOFG JBShsJqxOZ669HfRPt7OAD6trxfKb18wynk12lB5FdQ023KZqN0C9reXcfmgXND223HY CY4exj5BSr7r2FUzKAaeiNfGTfgRwM3Av4u5nb1OMKuyfYQDgupzaPrmDJnRd8JpgWPH G/0PCL48oMWsoENlP21gpdbgMBNjxfqL7EDjLj6ejHbpez5I790jC6qwU+YaJGP2+Dlw 0ormvEbE4TzoYsVI/pVsC+fIRkPqCCg522EeOzxOamabb/cH1e1mNjaz6BnrPJs6Vvz+ 4N9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682450487; x=1685042487; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dk0+KrWcmsCzCU9lXnwYuCuV6Pn3DqiOUPlx74ityKM=; b=CQQtkj4yp8nJY1sa7eFjG78Pa7sb9USVzdu5dXpPzIxdDZF16Z6E4a4vmYr1vcs976 T/uLfaXx+66u2N3bcudqbxCAAKkZB1iM9zBAndpd9+K4qnuMZ7kRxJ9HqronTFMySMin ng9Ml8EcJ1q5OcBK7miIHsg3yTdBlATvPNP8r0R7BpLOQzTCrmf7YgsnwJfg/R1JbSyv 7zFIxDf8We0I978ZxwV5bHoxxh3l8bHYkuACBv/4Kzu1E2bP6C0qjLtYxHpQOaGFQrIp nsWS2A+sF4bZyQXRF3jQmBzVSHwWV7FaSwz9rTl0M/0WKz2KdGZm/XacniZk1xDLpx/u qNRw== X-Gm-Message-State: AAQBX9cGfnjwjpVz8YHMfpPpCx1azGtErawKjDEaTzg4DnKw/ytNVDCN lsSnmf2kjqTNwWXx9rv30GeUFw== X-Google-Smtp-Source: AKy350afKcSldrvY3QZsJaStZawttfHie0lgskCh1w1RVSnk5yWTXj8XhFX4LOX4WNCTa7w988dMhg== X-Received: by 2002:a5d:560e:0:b0:2ce:a8e9:bb3c with SMTP id l14-20020a5d560e000000b002cea8e9bb3cmr13033111wrv.15.1682450486791; Tue, 25 Apr 2023 12:21:26 -0700 (PDT) Received: from [10.128.81.229] (bba-2-50-52-125.alshamil.net.ae. [2.50.52.125]) by smtp.gmail.com with ESMTPSA id f8-20020a5d4dc8000000b002f9e04459desm13698968wru.109.2023.04.25.12.21.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Apr 2023 12:21:26 -0700 (PDT) Message-ID: <7a24765b-89c3-97c2-c918-dfc0e18ae7cf@jguk.org> Date: Tue, 25 Apr 2023 20:21:24 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: zero length array example does not compile Content-Language: en-GB To: Jonathan Wakely Cc: GCC References: From: Jonny Grant In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,NICE_REPLY_A,RCVD_IN_BARRACUDACENTRAL,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 25/04/2023 13:22, Jonathan Wakely wrote: > On Tue, 25 Apr 2023 at 13:17, Jonathan Wakely wrote: >> >> On Tue, 25 Apr 2023 at 13:13, Jonny Grant wrote: >>> >>> Hello >>> >>> https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html >>> >>> I wondered 'this_length' refers to in that example, it doesn't compile. >> >> It's not supposed to be a complete program. >> >>> >>> : In function 'main': >>> :13:34: error: 'this_length' undeclared (first use in this function) >>> 13 | malloc (sizeof (struct line) + this_length); >>> | ^~~~~~~~~~~ >>> >>> >>> https://godbolt.org/z/PWEcWsrKv >>> >>> I probably the size of the struct? So that would be 4 bytes for me, as it is just the int. That doesn't seem very useful. Maybe I am missing something. >> >> Yes, you are. Look at how it's used: malloc is called to allocate >> sizeof(struct line) + this_length bytes. Why would it be the size of >> the struct? >> >> It's the number of bytes that the zero-length contents array can hold. > > Maybe this change would help: > > --- a/gcc/doc/extend.texi > +++ b/gcc/doc/extend.texi > @@ -1705,6 +1705,9 @@ struct line *thisline = (struct line *) > thisline->length = this_length; > @end smallexample > > +In this example, @code{thisline->contents} is an array of @code{char} that > +can hold up to @code{thisline->length} bytes. > + > Although the size of a zero-length array is zero, an array member of > this kind may increase the size of the enclosing type as a result of tail > padding. The offset of a zero-length array member from the beginning That looks like an improvement. Doesn't need to be a complete program, feels like a complete example is better. Adding this to the example would help: size_t this_length = 10; /* line has capacity for 10 char */