Re: Two questions on arrays with size defined by variables

Liste des GroupesRevenir à cl c 
Sujet : Re: Two questions on arrays with size defined by variables
De : noone (at) *nospam* noone.net (Andrey Tarasevich)
Groupes : comp.lang.c
Date : 09. Feb 2025, 16:15:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <voagra$mdud$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On Sun 2/9/2025 1:54 AM, Janis Papanagnou wrote:
On 09.02.2025 09:06, Andrey Tarasevich wrote:
On Sat 2/8/2025 11:50 PM, Janis Papanagnou wrote:
I've found examples on the Net where the arrays have been defined in a
function context and the size passed as parameter
>
    f(int n) {
       char * arr[n];
       ...
    }
>
Yes, that would be a VLA.
>
That reminded me on other languages where you'd need at least a block
context for dynamically sized arrays, like
>
    int n = 5;
    {
       char * arr[n];
       ...
    }
>
But a function body is in itself a block. Inside a function body you are
already in "a block context".
>
Anyway. I tried it without function or block context
>
    int n = 5;
    char * arr[n];
    ...
>
and it seemed to work seamlessly like that (with GNU cc, -std=C99).
>
You mean you did this at file scope? No, VLAs are illegal at file scope.
And I was unable to repeat this feat in GCC.
 Oh, sorry, no; above I had just written an excerpt. - Actually I had
those two examples above within a main() function. - Sorry again for
my inaccuracy.
 What I meant was (with surrounding context) that I knew (from _other_
languages) a syntax like
 main ()
{
   int n = 5;
    {
      char * arr[n];
      ...
   }
}
 And in "C" (C99) I tried it *without* the _inner block_
 main ()
{
   int n = 5;
   char * arr[n];
   ...
}
 and it seemed to work that way. (In those other languages that wasn't
possible.)
 
>
Q1: Is this a correct (portable) form?
>
VLA objects have to be declared locally. However, keep in mind that
support for local declarations of VLA _objects_ is now optional (i.e.
not portable). Support for variably-modified _types_ themselves (VLA
types) is mandatory. But you are not guaranteed to be able to declare an
actual VLA variable.
 I fear I don't understand what you're saying here. - By "now" do you
mean newer versions of the C standards?
Yes, I mean C23 specifically.

That you can rely only, say,
rely on it with C99 but maybe not before and not in later C standards
conforming compilers?
Things take some wild swings wrt to VLA support as you progress through various C standards beginning. Before C99 there was no VLA. In C99 everything VLA is required. In C11 everything VLA is optional. C23 takes a hybrid approach: the whole thing is required, except support for declaring automatic VLA objects is optional.
The latter means that you can declare VLA typedefs or VLA parameters in functions, apply `sizeof` to VLA types and expressions, but not necessarily declare such arrays locally
   void foo(int n, int m, int a[n][m])
   // VLA parameter (will be adjusted to pointer to VLA) - supported
   {
     typedef char A[n];
     // VLA typedef - supported
     double (*p)[n + m] = malloc(sizeof *p);
     // Pointer to VLA and `sizeof` on VLA - supported
     A x;
     short y[n] = {};
     // Both are automatic VLAs - optional
   }

For my purpose it would be okay to know whether with the C99 version
(that I used) it's okay, or whether that's some GNU specific extension
or some such.
Formally, if you want to declare local (automatic) VLAs, then the only version of C standard with which you are completely okay is C99. Later versions make things more problematic.
However, GNU seems to be dedicated to supporting everything VLA-related.
--
Best regards,
Andrey

Date Sujet#  Auteur
9 Feb 25 * Two questions on arrays with size defined by variables45Janis Papanagnou
9 Feb 25 `* Re: Two questions on arrays with size defined by variables44Andrey Tarasevich
9 Feb 25  +* Re: Two questions on arrays with size defined by variables41Janis Papanagnou
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables10Keith Thompson
9 Feb 25  ii`* Re: Two questions on arrays with size defined by variables9Janis Papanagnou
10 Feb 25  ii `* Re: Two questions on arrays with size defined by variables8Keith Thompson
10 Feb 25  ii  `* Re: Two questions on arrays with size defined by variables7Janis Papanagnou
10 Feb 25  ii   +- Re: Two questions on arrays with size defined by variables1James Kuyper
10 Feb 25  ii   `* Re: Two questions on arrays with size defined by variables5Andrey Tarasevich
10 Feb 25  ii    `* Re: Two questions on arrays with size defined by variables4Janis Papanagnou
10 Feb 25  ii     `* Re: Two questions on arrays with size defined by variables3Keith Thompson
10 Feb 25  ii      `* Re: Two questions on arrays with size defined by variables2Janis Papanagnou
10 Feb 25  ii       `- Re: Two questions on arrays with size defined by variables1Keith Thompson
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables6Michael S
9 Feb 25  ii`* Re: Two questions on arrays with size defined by variables5Janis Papanagnou
9 Feb 25  ii `* Re: Two questions on arrays with size defined by variables4Michael S
9 Feb 25  ii  +- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
10 Feb 25  ii  `* Re: Two questions on arrays with size defined by variables2Keith Thompson
10 Feb 25  ii   `- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables20Waldek Hebisch
9 Feb 25  ii`* Re: Two questions on arrays with size defined by variables19Janis Papanagnou
9 Feb 25  ii +* Re: Two questions on arrays with size defined by variables13Andrey Tarasevich
9 Feb 25  ii i`* Re: Two questions on arrays with size defined by variables12Janis Papanagnou
9 Feb 25  ii i +* Re: Two questions on arrays with size defined by variables7Janis Papanagnou
9 Feb 25  ii i i`* Re: Two questions on arrays with size defined by variables6James Kuyper
10 Feb 25  ii i i +- Re: Two questions on arrays with size defined by variables1Waldek Hebisch
10 Feb 25  ii i i `* Re: Two questions on arrays with size defined by variables4Janis Papanagnou
10 Feb 25  ii i i  `* Re: Two questions on arrays with size defined by variables3David Brown
10 Feb 25  ii i i   `* Re: Two questions on arrays with size defined by variables2Janis Papanagnou
10 Feb 25  ii i i    `- Re: Two questions on arrays with size defined by variables1David Brown
9 Feb 25  ii i `* Re: Two questions on arrays with size defined by variables4Michael S
10 Feb 25  ii i  `* Re: Two questions on arrays with size defined by variables3Opus
10 Feb 25  ii i   `* Re: Two questions on arrays with size defined by variables2Michael S
10 Feb 25  ii i    `- Re: Two questions on arrays with size defined by variables1Opus
10 Feb 25  ii `* Re: Two questions on arrays with size defined by variables5Keith Thompson
10 Feb 25  ii  `* Re: Two questions on arrays with size defined by variables4Janis Papanagnou
10 Feb 25  ii   `* Re: Two questions on arrays with size defined by variables3Janis Papanagnou
10 Feb 25  ii    `* Re: Two questions on arrays with size defined by variables2Keith Thompson
10 Feb 25  ii     `- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
9 Feb 25  i+* Re: Two questions on arrays with size defined by variables3Andrey Tarasevich
9 Feb 25  ii+- Re: Two questions on arrays with size defined by variables1Janis Papanagnou
9 Feb 25  ii`- Re: Two questions on arrays with size defined by variables1James Kuyper
9 Feb 25  i`- Re: Two questions on arrays with size defined by variables1James Kuyper
9 Feb 25  +- Re: Two questions on arrays with size defined by variables1James Kuyper
15 Feb17:19  `- Re: Two questions on arrays with size defined by variables1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal