• Re: Why are VLAs optional in C11?

    From pdx_scooter@yahoo.com@pdx_scooter@yahoo.com to comp.std.c on Sat Oct 1 13:15:01 2022
    From Newsgroup: comp.std.c

    On Saturday, August 18, 2012 at 5:33:19 AM UTC-7, jacob navia wrote:
    In my implementation [of VLA parameters] I had a REAL sizeof that will return the actual
    size. I was told to replace it, but I think I will not.
    An implementation of sizeof that returns something other than the size of the pointer when given a VLA parameter violates the fundamental rules of C pointer arithmetic. You cannot pass an array to a function in C, except by wrapping a struct or union around it. Confusingly, C allows you to write function parameters that -appear- to be arrays, e.g.
    int main(int argc, char *argv[]) // misleading declaration: argv is NOT an array; rather, it's merely a vector
    The compiler knows that an array cannot be passed directly to a function because any time an expression of type array of X appears in expression context (e.g. the actual parameter in a function call), the compiler promotes the type to pointer to X by taking the address of the first element. So when the compiler sees an array type in a parameter list, it silently re-writes it to what it really is:
    int main(int argc, char **argv)
    This is a throw-back to pre-ANSI C when the compiler would also silently re-write formal parameters of type "char" and "short" to "int" and "float" to "double". It's too bad ANSI C didn't disallow writing parameters that appear incorrectly as array types, but the fact that they didn't made VLA parameters possible.
    In my example, regardless of which declaration I use for argv, argv is a pointer in every way. For example, we can say ++argv. We wouldn't be able to increment it if it were an array.
    The VLA parameter is similarly silently rewritten as a pointer type, and just as above, when we have:
    void f(int n, int a[n])
    We can say ++a inside the function.
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.std.c on Sat Oct 1 14:33:14 2022
    From Newsgroup: comp.std.c

    "pdx_scooter@yahoo.com" <pdx_scooter@yahoo.com> writes:
    On Saturday, August 18, 2012 at 5:33:19 AM UTC-7, jacob navia wrote:

    In my implementation [of VLA parameters] I had a REAL sizeof that will return the actual
    size. I was told to replace it, but I think I will not.

    An implementation of sizeof that returns something other than the size
    of the pointer when given a VLA parameter violates the fundamental
    rules of C pointer arithmetic.
    [...]

    Agreed.

    Are you aware that you just replied to an article that was posted 10
    years ago?
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    Working, but not speaking, for Philips
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Tomasz Stanislawski@stanislawski.tomasz@googlemail.com to comp.std.c on Wed Jan 25 04:21:31 2023
    From Newsgroup: comp.std.c

    void f(int n, int a[n])

    We can say ++a inside the function.

    There is some debate if it is an actual VLA type. The standard requires to transform the declaration of `int a[n]` to `int* a`.
    To my understanding, the original VLA type is erased during at function's *definition* and its top-level size expression should not never be evaluated.
    The situation is different while passing a pointer to an array:

    ```
    void f(int n, int (*a)[n]) { ... }
    ```

    Now, `n` must be evaluated at every invocation of the function.
    --- Synchronet 3.20a-Linux NewsLink 1.113