From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Jonny Grant <jg@jguk.org>
Cc: GCC <gcc@gcc.gnu.org>
Subject: Re: zero length array example does not compile
Date: Tue, 25 Apr 2023 13:22:49 +0100 [thread overview]
Message-ID: <CAH6eHdRYo8CX=m-K4qjjEF8-X1UmrL_=2BBCcqZSap7grD+tOg@mail.gmail.com> (raw)
In-Reply-To: <CAH6eHdT=OnOOOuBdE1dD1x2CfcsBvNm-4r70y=JeEeFi3BRTLA@mail.gmail.com>
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.
>
> >
> > <source>: In function 'main':
> > <source>: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
next prev parent reply other threads:[~2023-04-25 12:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-25 12:13 Jonny Grant
2023-04-25 12:17 ` Jonathan Wakely
2023-04-25 12:22 ` Jonathan Wakely [this message]
2023-04-25 19:21 ` Jonny Grant
2023-04-25 20:04 ` Jonathan Wakely
2023-04-26 11:26 ` Jonny Grant
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAH6eHdRYo8CX=m-K4qjjEF8-X1UmrL_=2BBCcqZSap7grD+tOg@mail.gmail.com' \
--to=jwakely.gcc@gmail.com \
--cc=gcc@gcc.gnu.org \
--cc=jg@jguk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).