Variable Length Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cromulent

    Variable Length Arrays

    In section 6.7.5.2 it states the following:

    If the size is not present, the array type is an incomplete type. If
    the size is*instead of
    being an expression, the array type is a variable length array type of
    unspecified size,
    which can only be used in declarations with function prototype
    scope;124) such arrays are
    nonetheless complete types. If the size is an integer constant
    expression and the element
    type has a known constant size, the array type is not a variable length
    array type;
    otherwise, the array type is a variable length array type.

    5 If the size is an expression that is not an integer constant
    expression: if it occurs in a
    declaration at function prototype scope, it is treated as if it were
    replaced by*;otherwise,
    each time it is evaluated it shall have a value greater than zero. The
    size of each instance
    of a variable length array type does not change during its lifetime.
    Where a size
    expression is part of the operand of a sizeof operator and changing the
    value of the
    size expression would not affect the result of the operator,
    itisunspecifie d whether or not
    the size expression is evaluated.

    but I am having problems understanding the purpose of an incomplete
    type. What is the purpose and why would someone use one rather than
    specifying an int for the size of the VLA?

    How does it affect the operation of the array? Does it change how it is
    treated by the program?

    Thanks for any help.

    --
    "I disapprove of what you say, but I'll defend to the death your right
    to say it." - Voltaire

  • Cromulent

    #2
    Re: Variable Length Arrays

    On 2008-08-05 10:04:30 +0100, Cromulent <cromulent@just extrememetal.co msaid:
    <snip>
    I am assuming from the lack of responses that I am being increadibly
    stupid with that question.

    Okay let me rephrase the question is this the correct interpretation of
    the standard text quoted in my original post?

    int blah_func(int somesize, int arr[*]); /* This is the only legal use
    of * in an array declaration */

    int blah_func(int somesize, int arr[somesize])
    {

    /* arr[] is an array of variable size specified by the integer somesize */

    return 0;
    }

    I'm just trying to find out what I am missing here really.

    --
    "I disapprove of what you say, but I'll defend to the death your right
    to say it." - Voltaire

    Comment

    • Walter Roberson

      #3
      Re: Variable Length Arrays

      In article <20080805100430 16807-cromulent@juste xtrememetalcom> ,
      Cromulent <cromulent@just extrememetal.co mwrote:
      >but I am having problems understanding the purpose of an incomplete
      >type. What is the purpose and why would someone use one rather than
      >specifying an int for the size of the VLA?
      VLAs are new in C99; incomplete types are the protocol for C95
      and earlier.

      Besides, you don't always know the array size accurately.
      The purpose of the routine might be to -determine- the array
      size by searching for a particular marker (e.g., strlen)
      --
      "I buy more from my grocer than he buys from me, and I bet it's
      the same with you and your grocer. That means we have a trade
      deficit with our grocers. Does our perpetual grocer trade deficit
      portend doom?" -- Walter Williams

      Comment

      • Ben Bacarisse

        #4
        Re: Variable Length Arrays

        Cromulent <cromulent@just extrememetal.co mwrites:
        On 2008-08-05 10:04:30 +0100, Cromulent <cromulent@just extrememetal.co msaid:
        >
        ><snip>
        >
        I am assuming from the lack of responses that I am being increadibly
        stupid with that question.
        >
        Okay let me rephrase the question is this the correct interpretation
        of the standard text quoted in my original post?
        >
        int blah_func(int somesize, int arr[*]); /* This is the only legal use
        of * in an array declaration */
        All it is there for is so that you don't have to repeat the parameter
        name that gives the size. It also gives you a notation to use when
        you don't want any names, but that only pays off with 2D arrays.

        In your case:

        int blah_func(int somesize, int arr[somesize]);
        int blah_func(int, int arr[*]);
        int blah_func(int, int arr[]);
        int blah_func(int, int *arr);

        are all the same, but with a 2D array you need to say that the size of
        the row is determined by a parameter. I.e.:

        f(int, int, int (*)[*]);
        f(int n, int m, int (*)[m]);
        f(int n, int m, int mat[n][m]);
        f(int n, int m, int mat[*][*]);
        f(int, int, int mat[*][*]);

        are all the same, but not at all the same as:

        f(int, int, int **);
        f(int n, int m, int mat[][]);

        The first is just another function altogether and the second is not
        valid as a declaration. The * lets to show the "arrayness" of a
        multidimensiona l array parameter without having to repeat (or
        specify) the names of the sizes. I imagine the most common cases
        you'll see are:

        f(int n, int m, int mat[*][*]);
        f(int, int, int mat[*][*]);

        and sometimes:

        f(int, int, int (*)[*]);
        I'm just trying to find out what I am missing here really.
        You need to ask a more specific question.

        --
        Ben.

        Comment

        • Flash Gordon

          #5
          Re: Variable Length Arrays

          Cromulent wrote, On 05/08/08 17:25:
          On 2008-08-05 10:04:30 +0100, Cromulent <cromulent@just extrememetal.co m>
          said:
          >
          ><snip>
          >
          I am assuming from the lack of responses that I am being increadibly
          stupid with that question.
          No, the reason for lack of response is that you have not allowed enough
          time. For example I was driving to a customer site when you first
          posted, worked there without internet access, drove back to my office
          where I had to do my expenses and whilst I was doing my expenses you
          made this second post.
          Okay let me rephrase the question is this the correct interpretation of
          the standard text quoted in my original post?
          >
          int blah_func(int somesize, int arr[*]); /* This is the only legal use
          of * in an array declaration */
          Yes, that is my reading of what you quoted from the standard as well.
          int blah_func(int somesize, int arr[somesize])
          {
          >
          /* arr[] is an array of variable size specified by the integer
          somesize */
          No, it is the same as if you had used "int add[*]" because of this part
          of what you posted...
          | 5 If the size is an expression that is not an integer constant
          | expression: if it occurs in a declaration at function prototype scope,
          | it is treated as if it were replaced by*;
          return 0;
          }
          >
          I'm just trying to find out what I am missing here really.
          See above for at least one thing you were missing.

          You also asked about incomplete types in your original post. There are a
          completely different kettle of fish from your examples above.

          You could do something like the following:

          /* foo.h */
          extern int arr[]; /* incomplete */

          /* foo.c */
          #include "foo.h"
          int arr[5];

          Now other files can include foo.h and they will not know how big the
          array is.
          --
          Flash Gordon

          Comment

          • Cromulent

            #6
            Re: Variable Length Arrays

            On 2008-08-05 19:40:10 +0100, Flash Gordon <spam@flash-gordon.me.uksai d:
            Cromulent wrote, On 05/08/08 17:25:
            >On 2008-08-05 10:04:30 +0100, Cromulent <cromulent@just extrememetal.co msaid:
            >>
            >><snip>
            >>
            >I am assuming from the lack of responses that I am being increadibly
            >stupid with that question.
            >
            No, the reason for lack of response is that you have not allowed enough
            time. For example I was driving to a customer site when you first
            posted, worked there without internet access, drove back to my office
            where I had to do my expenses and whilst I was doing my expenses you
            made this second post.
            Ah, sorry. Patience is not a virtue I seem to have been blessed with :).
            >
            >Okay let me rephrase the question is this the correct interpretation of
            >the standard text quoted in my original post?
            >>
            >int blah_func(int somesize, int arr[*]); /* This is the only legal use
            >of * in an array declaration */
            >
            Yes, that is my reading of what you quoted from the standard as well.
            >
            >int blah_func(int somesize, int arr[somesize])
            >{
            >>
            > /* arr[] is an array of variable size specified by the integer somesize */
            >
            No, it is the same as if you had used "int add[*]" because of this part
            of what you posted...
            | 5 If the size is an expression that is not an integer constant
            | expression: if it occurs in a declaration at function prototype scope,
            | it is treated as if it were replaced by*;
            >
            > return 0;
            >}
            >>
            >I'm just trying to find out what I am missing here really.
            >
            See above for at least one thing you were missing.
            Hmm okay. Thanks for pointing that out. I think I see how these work now.
            >
            You also asked about incomplete types in your original post. There are
            a completely different kettle of fish from your examples above.
            >
            You could do something like the following:
            >
            /* foo.h */
            extern int arr[]; /* incomplete */
            >
            /* foo.c */
            #include "foo.h"
            int arr[5];
            >
            Now other files can include foo.h and they will not know how big the array is.
            This is what I get lost with. What is the point of declaring an array,
            and then initilising it and then being able to use the same array in
            another file which has no idea of the size of the array?

            Thank you both for the help.
            --
            "I disapprove of what you say, but I'll defend to the death your right
            to say it." - Voltaire

            Comment

            • Chris Torek

              #7
              Re: Variable Length Arrays

              In article <20080805221643 16807-cromulent@juste xtrememetalcom>
              Cromulent <cromulent@just extrememetal.co mwrote:
              >... What is the point of declaring an array, and then initilising
              >it and then being able to use the same array in another file which
              >has no idea of the size of the array?
              Here is an example of how one might use that:

              % cat file1.c
              #include "header.h"

              void do_something(vo id) {
              size_t i;

              for (i = 0; i < size_of_array; i++)
              operate(array[i]);
              }
              /* presumably more stuff here */
              % cat file2.c
              #include "header.h"

              double array[] = {
              3.1415926535897 932384626433832 7950,
              2.7182818284590 452353602874713 5266,
              42.0
              };

              size_t size_of_array = sizeof array / sizeof *array;
              /* possibly more stuff here */
              % cat header.h
              #include <stddef.h>
              extern double array[];
              extern size_t size_of_array;

              void do_something(vo id);
              void operate(double) ;
              /* more stuff here as needed */
              %

              You can now change the behavior of the program by changing only
              file2.c (and recompiling appropriate files), without having to
              change file1.c (which, if this were a more realistic example, might
              be quite a bit larger than shown above).
              --
              In-Real-Life: Chris Torek, Wind River Systems
              Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
              email: gmail (figure it out) http://web.torek.net/torek/index.html

              Comment

              • Cromulent

                #8
                Re: Variable Length Arrays

                On 2008-08-06 10:07:39 +0100, Chris Torek <nospam@torek.n etsaid:
                In article <20080805221643 16807-cromulent@juste xtrememetalcom>
                Cromulent <cromulent@just extrememetal.co mwrote:
                >... What is the point of declaring an array, and then initilising
                >it and then being able to use the same array in another file which
                >has no idea of the size of the array?
                >
                <snip>
                >
                You can now change the behavior of the program by changing only
                file2.c (and recompiling appropriate files), without having to
                change file1.c (which, if this were a more realistic example, might
                be quite a bit larger than shown above).
                Ah ha! Thanks, so it is a way of producing modular code then that can
                act on an array without having the specifics of said array then?
                --
                "I disapprove of what you say, but I'll defend to the death your right
                to say it." - Voltaire

                Comment

                Working...