zero length array example does not compile

Jonathan Wakely jwakely.gcc@gmail.com
Tue Apr 25 12:17:04 GMT 2023


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.


More information about the Gcc mailing list