public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* zero length array example does not compile
@ 2023-04-25 12:13 Jonny Grant
  2023-04-25 12:17 ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Jonny Grant @ 2023-04-25 12:13 UTC (permalink / raw)
  To: GCC

Hello

https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

I wondered 'this_length' refers to in that example, it doesn't compile.

<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.

Kind regards
Jonny

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: zero length array example does not compile
  2023-04-25 12:13 zero length array example does not compile Jonny Grant
@ 2023-04-25 12:17 ` Jonathan Wakely
  2023-04-25 12:22   ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2023-04-25 12:17 UTC (permalink / raw)
  To: Jonny Grant; +Cc: GCC

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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: zero length array example does not compile
  2023-04-25 12:17 ` Jonathan Wakely
@ 2023-04-25 12:22   ` Jonathan Wakely
  2023-04-25 19:21     ` Jonny Grant
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2023-04-25 12:22 UTC (permalink / raw)
  To: Jonny Grant; +Cc: GCC

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: zero length array example does not compile
  2023-04-25 12:22   ` Jonathan Wakely
@ 2023-04-25 19:21     ` Jonny Grant
  2023-04-25 20:04       ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Jonny Grant @ 2023-04-25 19:21 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: GCC



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.
>>
>>>
>>> <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

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 */


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: zero length array example does not compile
  2023-04-25 19:21     ` Jonny Grant
@ 2023-04-25 20:04       ` Jonathan Wakely
  2023-04-26 11:26         ` Jonny Grant
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2023-04-25 20:04 UTC (permalink / raw)
  To: Jonny Grant; +Cc: GCC

On Tue, 25 Apr 2023 at 20:21, Jonny Grant <jg@jguk.org> wrote:
>
>
>
> 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.
> >>
> >>>
> >>> <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
>
> 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 */

That seems to prompt more questions though. Why 10 not another number?
Why size_t not the same type as the line.length member? If you have a
hardcoded 10 why not just use an array or 10 char in the struct?

So I'm not convinced your change improves it at all. The specific
value and the specific type are irrelevant when what's needed is just
some number. It isn't actually declared in the example because it's
not actually relevant to the thing being demonstrated.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: zero length array example does not compile
  2023-04-25 20:04       ` Jonathan Wakely
@ 2023-04-26 11:26         ` Jonny Grant
  0 siblings, 0 replies; 6+ messages in thread
From: Jonny Grant @ 2023-04-26 11:26 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: GCC



On 26/04/2023 00:04, Jonathan Wakely wrote:
> On Tue, 25 Apr 2023 at 20:21, Jonny Grant <jg@jguk.org> wrote:
>>
>>
>>
>> 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.
>>>>
>>>>>
>>>>> <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
>>
>> 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 */
> 
> That seems to prompt more questions though. Why 10 not another number?

Just an example, I feel examples are better when they show the variables. That number would be at local scope in a function that wanted to make use of the struct, rather than hardcoded in header with the struct.

Personally I would name it differently too:
size_t content_length = 10; /* line has capacity for this many char */

> Why size_t not the same type as the line.length member?

My implication was the example shouldn't use an int. size_t for memory byte count would follow the convention of the APIs: 
void *malloc(size_t size);
operator new[]


> If you have a> hardcoded 10 why not just use an array or 10 char in the struct?
> 
> So I'm not convinced your change improves it at all. The specific
> value and the specific type are irrelevant when what's needed is just
> some number. It isn't actually declared in the example because it's
> not actually relevant to the thing being demonstrated.

Maybe say it needs to be "just some number" then, (that isn't shown in the example).

I'll leave it with you. Your change looked good.
Thank you again for your replies.
Jonny

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-04-26 11:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-25 12:13 zero length array example does not compile Jonny Grant
2023-04-25 12:17 ` Jonathan Wakely
2023-04-25 12:22   ` Jonathan Wakely
2023-04-25 19:21     ` Jonny Grant
2023-04-25 20:04       ` Jonathan Wakely
2023-04-26 11:26         ` Jonny Grant

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).