Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1539

Re: Why are VLAs optional in C11?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Re: Why are VLAs optional in C11?
Date 2012-08-18 13:48 -0700
Organization None to speak of
Message-ID <lntxw0lyf3.fsf@nuthaus.mib.org> (permalink)
References <lnfw7lnnrh.fsf@nuthaus.mib.org> <502ED178.50603@loria.fr> <k0n48g$tsc$1@dont-email.me> <k0o22h$5ni$1@speranza.aioe.org>

Show all headers | View raw


jacob navia <jacob@spamsink.net> writes:
> Le 18/08/12 06:04, John Nagle a écrit :
>> On 8/17/2012 4:19 PM, Jens Gustedt wrote:
>>> Am 18.08.2012 00:42, schrieb Keith Thompson:
>>>> Why did C11 make VLAs optional?
>> ...
>>> It is really unfortunate, that we don't have the rationale document
>>> for C11, yet.
>>
>>     True.  Here are some of the known problems with VLAs:
>>
>> - Putting arbitrarily large arrays on the stack causes trouble
>> in multithreaded programs in implementations where stack growth
>> is bounded.
>>
>
> This is not specific to VLAs. In general VLAs REDUCE stack growth.
>
> Without VLAs you write:
>
> #define MAXELEMENTS 512
>
> int fn(void)
> {
> 	int table[MAXELEMENT];
> }
>
>
> MOST cases will be 10-20, some cases it would go to 100, and the
> programmer, careful to avoid overflows decides 512.
>
> With VLAs in most cases you will just use 10-20 elements, not one more 
> as it is NEEDED. There is no need for an arbitrary maximum.

Without VLAs, I'd probably use malloc().

VLAs can either increase or reduce stack growth, depending on how you
use them.

[...]

>> - Microsoft declines to support them.
[...]
> o Microsoft doesn't like the C language at all. Let's close shop then.
>    Microsoft may have a point. There is no need for a C committee if we
>    have Microsoft.
>
>    WHAT?
>
>    You are again MICROSOFT, the software giant? You must be nuts.

This isn't about anybody being against anybody.  Microsoft exists and
is significant.  There are other C compilers for Windows, but using
one of them may not be an option for some projects (for example,
management might insist on using only Microsoft development tools).
Conclusion: *some* C programmers (those who need to use Microsoft
C compilers) are unable to use most C99 and C11 features.  I don't
like it any more than you do, but sarcasm isn't going to change it --
and it may be quite relevant to this discussion.

>> - VLAs aren't used much.  There appear to be only three in
>> Google Code, and no VLA parameters. The Linux kernel had one,
>> but it was taken out because there was no way to handle an
>> out of space condition.  (If anyone can find an example of
>> a VLA parameter in publicly visible production code, please
>> let me know.)
>>
>
> Just one example:
>
> struct foo;
>
> This is an opaque definition of a data type, published by a library
> that keeps the internals hidden.
>
> problem: You can't make a local variable of type "foo" since the size
> isn't know. If the library provides a library function
>
> size_t SizeofFoo(void);
>
> You can declare a local variable of type foo with
>
> int FunctionThatUsesAFoo(void)
> {
> 	char myFooBuf[SizeofFoo()];
> 	struct foo *myFoo = &myFooBuf;
> }

Using malloc instead of an automatic char array would avoid alignment
problems.
[...]

>> - The semantics of VLA parameters is painful.  They're
>> automatically reduced to pointers, with the length information
>> lost.  "sizeof" returns the size of a pointer.
>>
>
> In my implementation I had a REAL sizeof that will return the actual
> size. I was told to replace it, but I think I will not.

You should at least fix it.  With the version of lcc-win64 I installed
on June 9, this program:

    #include <stdio.h>
    
    void func(int n, int param[n]) {
        printf("sizeof param = %zu\n", sizeof param);
    }
    
    int main(void) {
        enum { LEN = 10 };
        int obj[LEN];
        printf("sizeof obj   = %zu\n", sizeof obj);
        func(LEN, obj);
        return 0;
    }
    
produces this output:

    sizeof obj   = 40
    sizeof param = 10

A conforming C99 or C11 compiler must compute `sizeof param` to
be the same as `sizeof (int*)`, since param is a pointer.  If,
as you suggest, `sizeof param` should instead be the size of the
argument, then it should be 40 rather than 10.  If you intend it to
be the number of elements rather than the size in bytes, I suggest
introducing a new implementation-defined operator, perhaps something
like `_Lengthof`.  (It's between you and your customers to decide
whether lcc-win needs to be conforming.)

(Incidentally, `...` is the Markdown syntax for code samples; see
<http://en.wikipedia.org/wiki/Markdown>.  I think I'm going to start
using it here.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.std.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-17 15:42 -0700
  Re: Why are VLAs optional in C11? Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-18 01:19 +0200
    Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-17 16:23 -0700
      Re: Why are VLAs optional in C11? Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-18 10:12 +0200
    Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-17 21:04 -0700
      Re: Why are VLAs optional in C11? pacman@kosh.dhis.org (Alan Curry) - 2012-08-18 05:34 +0000
        Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-17 23:07 -0700
          Re: Why are VLAs optional in C11? pacman@kosh.dhis.org (Alan Curry) - 2012-08-18 08:29 +0000
            Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-18 09:37 -0700
              Re: Why are VLAs optional in C11? pa@see.signature.invalid (Pierre Asselin) - 2012-08-19 02:27 +0000
            Re: Why are VLAs optional in C11? Ike Naar <ike@sverige.freeshell.org> - 2012-08-18 17:49 +0000
      Re: Why are VLAs optional in C11? Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-18 08:32 +0200
      Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-18 02:50 -0700
      Re: Why are VLAs optional in C11? jacob navia <jacob@spamsink.net> - 2012-08-18 14:33 +0200
        Re: Why are VLAs optional in C11? Ike Naar <ike@sverige.freeshell.org> - 2012-08-18 13:23 +0000
        Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-18 13:48 -0700
          Re: Why are VLAs optional in C11? jacob navia <jacob@spamsink.net> - 2012-08-18 23:31 +0200
            Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-18 18:14 -0700
              Re: Why are VLAs optional in C11? jacob navia <jacob@spamsink.net> - 2012-08-19 09:38 +0200
                Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-19 03:07 -0700
                Re: Why are VLAs optional in C11? jacob navia <jacob@spamsink.net> - 2012-08-19 12:29 +0200
                Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-19 10:38 -0700
                Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-19 13:14 -0700
                Re: Why are VLAs optional in C11? Keith Thompson <kst-u@mib.org> - 2012-08-19 13:59 -0700
                Re: Why are VLAs optional in C11? Owen Shepherd <nntp@owenshepherd.net> - 2012-08-27 22:17 +0100
                Re: Why are VLAs optional in C11? Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-28 00:34 +0200
                Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-27 19:36 -0700
                Re: Why are VLAs optional in C11? James Kuyper <jameskuyper@verizon.net> - 2012-08-27 22:54 -0400
                Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-27 22:39 -0700
        Re: Why are VLAs optional in C11? "pdx_scooter@yahoo.com" <pdx_scooter@yahoo.com> - 2022-10-01 13:15 -0700
          Re: Why are VLAs optional in C11? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-10-01 14:33 -0700
          Re: Why are VLAs optional in C11? Tomasz Stanislawski <stanislawski.tomasz@googlemail.com> - 2023-01-25 04:21 -0800
  Re: Why are VLAs optional in C11? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-17 20:54 -0400
  Re: Why are VLAs optional in C11? John Nagle <nagle@animats.com> - 2012-08-17 20:56 -0700

csiph-web