C++ Logo

std-proposals

Advanced search

Re: Arrays (VLAs) as function parameters (as in C99)

From: Alejandro Colomar (man-pages) <"Alejandro>
Date: Sun, 14 Nov 2021 20:41:52 +0100
Hi Thiago,

On 11/14/21 20:06, Thiago Macieira via Std-Proposals wrote:
> On Friday, 12 November 2021 13:30:26 PST Alejandro Colomar (man-pages) via
> Std-Proposals wrote:
>>> #if __STDC_VERSION__ >= 199901L
>>> # define size_at_least(n) static n
>>> # define size_exactly(n) static restrict n
>>> #else
>>> # define size_at_least(n)
>>> # define size_exactly(n)
>>> #endif
>>>
>>> void process_array(size_t count, char payload[size_at_least(count)]);
>>
>> I didn't think of it that way, but yes, it makes sense.
>> I only thought about making C syntax (without macros hiding the stuff)
>> compile in C++.
>>
>> BTW, 'static restrict n' doesn't mean size_exactly() unless I'm also a
>> bit blind here. But yes, that concept makes sense.
>
> I think I got the syntax inverted. "static n" means "exactly n" and then they
> added the "restrict" to indicate it's at least that many.
>
> Shows you how little this gets used. And it's not just the fact that I'm a C++
> developer primarily -- if I ask my colleagues who code mostly in C, I bet you
> they won't know.
>

Yup, it's a weird and rarely-used syntax
(IMHO, because it's not very well designed,
and also because it's ignored by all compilers).
Let me clarify it:

'[static n]' means that both:

  - The pointer cannot be NULL.
  - the array has at least 'n' elements.

'[n]' means:

  - nothing at all. 'n' is ignored.

'[restrict n]' means:

  - 'n' is ignored.
  - the storage may not overlap any other 'restrict' pointers or arrays.

'[static restrict n]' means:

  - The pointer cannot be NULL.
  - the array has at least 'n' elements.


See:
   6.7.6.2 Array declarators
   6.7.6.3 Function declarators



I'd like to simplify that in C, and propose that

'[n]' means:

  - The array has at least 'n' elements.

Note that the pointer can still be NULL in this case,
per my definition.
For that I also want to add the '[[nonnull]]' attribute to ISO C
(and also to ISO C++).

That way, the following would be equivalent:

[[nonnull]]
int foo(ptrdiff_t n, int x[n]);

int foo(ptrdiff_t n, int x[static n]);

But that goes for long, maybe C3X.

My guess (and it's just a guess) is that the C committee considered that
giving a meaning to '[n]' when it had always been ignored
might break code.
My opinion is that the only code it can break is:

int foo(ptrdiff_t n, int x[n])
{
 return x[n + 5];
}

That program was (and is) "legal" C,
but that doesn't make it any less insane.
If that stops compiling, let's celebrate it!


All of this makes me wonder:

Does C++ want to mirror C here in the meaning of 'static'?
Or does it want to implement a slightly less ugly way of
saying "at least 'n' elements"
(i.e., [n] instead of [static n])?

If C++ implements VLAs, but doesn't implement static,
it would already be compatible with most C code,
since most C code doesn't make use of 'static'.
And it would also "help" C deprecate that ugly 'static',
by some better design, IMHO.

'static' could be nevertheless ignored as QoI by compilers,
or '#define static ' could be used by users as
I for example already do with 'restrict'.


Thanks,
Alex

Received on 2021-11-14 13:41:56