Talk:Variable-length array

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject C/C++  
WikiProject icon This article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C/C++ on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
 ???  This article has not yet received a rating on the quality scale.
 ???  This article has not yet received a rating on the importance scale.
 
WikiProject Computing  
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
 ???  This article has not yet received a rating on the project's quality scale.
 ???  This article has not yet received a rating on the project's importance scale.
 

stored where?[edit]

i think it is only on stack. but there might be cases where it is on heap due to size or just language way of dealing with ADT's. i ask for i added that it is typycally on stack onto main page. 84.16.123.194 (talk) 20:01, 23 October 2008 (UTC)

Depends on the language and implementation. In C (C99), it's on the stack, i.e., VLAs can only have auto storage class. In COBOL, it's either the WORKING-STORAGE or FILE SECTIONs, which are static data areas, or in the LINKAGE SECTION, which may be static to another program module or allocated on the heap (e.g., using EXEC CICS GETMAIN). — Loadmaster (talk) 20:19, 23 October 2008 (UTC)
thanks. should it be mentioned here? 84.16.123.194 (talk) 21:33, 23 October 2008 (UTC)

Only automatic?[edit]

I wouldn't say that the term "variable length array" is limited to any one storage duration. I would consider the term entirely appropriate for any array of non-fixed size, including arrays allocated out of a heap or arrays (as in awk and Perl) with no fixed size. (In fact, I would consider the awk/Perl to be *true* variable-length arrays, whereas C99 has fixed-length arrays with the length determined at execution time.) Jordan Brown (talk) 21:03, 23 April 2009 (UTC)

Of course, the length of dynamically allocated arrays (including resizable/dynamic arrays) doesn't have to be known at compile time, and you could trivially consider them to have a "variable length". But they are not what the term "Variable-length array" refers to, at least in the context of the C language. -- memset (talk) 12:21, 24 April 2009 (UTC)
But the article mentions lanugages other than C, too. What to do? We don't want to duplicate the Dynamic memory allocation article. — Loadmaster (talk) 16:30, 14 October 2009 (UTC)

your definition of "variable length"[edit]

the suggested definition of "variable length" is given without justification and without citation of sources. in fact the entire article is completely lacking in citations. citing sources is a requirement in Wikipedia.

to me, "variable length" means "resizable", a much more interesting topic and one which should occupy the position taken by this article which appears to only describe fixed size arrays whose length is specified once at runtime as opposed to at compile time. a very statistically poor straw poll conducted this morning showed that I'm not alone in assuming that the title implies "dynamically resizable".

the article needs to begin with a clarification and two principal sections, fixed size versus resizable. anyone willing to help" ?CecilWard (talk) 12:19, 17 June 2011 (UTC)

About the C example[edit]

I think the C example make confusion. It is wrote that vals[i] is passed to another function but in the true it is indicate to be equal to the result of the another function that is supposed to return a float value. If that is wrote is true, it should be:

read_val(vals[i]);

considering true:

read_val(float x)

and not:

vals[i] = read_val();

that, probably, make the entire array vals to be equal to one single float value.

Another thing, i do not understand what is its return:

return process(vals, n);

has no sense.

So, i think, the following is better:

void read_and_process(int n) {

   int i;
   float vals[n];
   printf("\nInsert %d float values:\n", n);
   for(i = 1; i <= n; i++) {
       scanf("%f", &vals[i]);
       read_val(vals[i]);
       puts("");
   }

}

that is a procedure (return void type), considering true:

read_val(float x)

The point of the C example is to illustrate how a VLA is allocated within a function, and then passed to another function.
float read_and_process(int n)
{
    float vals[n];
 
    for (int i = 0; i < n; i++)
        vals[i] = read_val();
    return process(vals, n);
}
Your proposed example does not show the second part, the passing of a VLA to another function. It's not important what the example code actually does, but that it illustrates the concept and use of VLAs. Note that we don't specify what function read_val() does, but presumably it reads a series of floating-point values, perhaps from some external source such as a data file or network. — Loadmaster (talk) 00:10, 25 April 2013 (UTC)

The goal of example should be to make clear how to insert and define an index of an array during the run of the program, in dynamic manner. Those values of the array can be used in many cases, for example passing them to a function, but not only. That example does not explain anything, especially in the last part; passing val and the index n to a function process, values that gets from read_val(), does not make any sense. If should be possible to write an example that asks for some int numbers, example: "how many?" and then asks for each and then pass all to a function that make their sum. It makes sense and makes clear what means the variable-length array and what it needs. — Preceding unsigned comment added by 151.46.26.229 (talk) 20:50, 29 April 2013 (UTC)