Variables as array size declarators.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ayaz Ahmed Khan

    Variables as array size declarators.

    Can a variable containing, say, an integer value be used as size
    declarator for an array of integers? g++ does not complain, but other
    compilers, such as Visual C++/6.0, Borland Builder C++, Turbo C++, and
    Dev-C++/4.0, that I tested it on decline to use variables as size
    declarators.


    --
    Ayaz Ahmed Khan

    Yours Forever in, | Webmaster,
    Cyberspace. | http://fast-ce.org/
    _______________ _______________ _______________ __
    I ditched Windows for Linux over two years ago.
    Life's never been better since.

  • Jakob Bieling

    #2
    Re: Variables as array size declarators.

    "Ayaz Ahmed Khan" <resilient@myre albox.com> wrote in message
    news:pan.2004.0 2.26.15.51.50.7 45504.1849@myre albox.com...[color=blue]
    > Can a variable containing, say, an integer value be used as size
    > declarator for an array of integers? g++ does not complain, but other
    > compilers, such as Visual C++/6.0, Borland Builder C++, Turbo C++, and
    > Dev-C++/4.0, that I tested it on decline to use variables as size
    > declarators.[/color]


    Not sure, but I think C allows this and g++ allows it as an extension.
    However, it is not part of Standard C++. If you want to use this kind of
    stuff, use the std::vector container class. It will do exactly what you
    want.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • Rolf Magnus

      #3
      Re: Variables as array size declarators.

      Ayaz Ahmed Khan wrote:
      [color=blue]
      > Can a variable containing, say, an integer value be used as size
      > declarator for an array of integers?[/color]

      No.
      [color=blue]
      > g++ does not complain, but other compilers, such as Visual C++/6.0,
      > Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I tested it on
      > decline to use variables as size declarators.[/color]

      You probably didn't use g++ in ansi/iso mode. Try to use at least the
      following command line switches:

      -ansi -pedantic -W -Wall

      Comment

      • Default User

        #4
        Re: Variables as array size declarators.

        Jakob Bieling wrote:[color=blue]
        >
        > "Ayaz Ahmed Khan" <resilient@myre albox.com> wrote in message
        > news:pan.2004.0 2.26.15.51.50.7 45504.1849@myre albox.com...[color=green]
        > > Can a variable containing, say, an integer value be used as size
        > > declarator for an array of integers?[/color][/color]

        [color=blue]
        > Not sure, but I think C allows this and g++ allows it as an extension.
        > However, it is not part of Standard C++. If you want to use this kind of
        > stuff, use the std::vector container class. It will do exactly what you
        > want.[/color]


        These (variable-length arrays) were added to C in the latest standard.
        As there are few compilers actually implementing that standard yet,
        actually having that capability in a standard-compliant way is spotty.



        Brian Rodenborn

        Comment

        • Ayaz Ahmed Khan

          #5
          Re: Variables as array size declarators.

          "Jakob Bieling" typed:
          [color=blue]
          > "Ayaz Ahmed Khan" <resilient@myre albox.com> wrote in message
          > news:pan.2004.0 2.26.15.51.50.7 45504.1849@myre albox.com...[color=green]
          >> Can a variable containing, say, an integer value be used as size
          >> declarator for an array of integers? g++ does not complain, but
          >> other compilers, such as Visual C++/6.0, Borland Builder C++, Turbo
          >> C++, and Dev-C++/4.0, that I tested it on decline to use variables
          >> as size declarators.[/color]
          >
          >
          > Not sure, but I think C allows this and g++ allows it as an
          > extension.
          > However, it is not part of Standard C++. If you want to use this
          > kind of stuff, use the std::vector container class. It will do
          > exactly what you want.[/color]


          I had been under the impression that it was a feature incorporated
          into standard C++. I found a reference to VLAs[1], but am not sure what
          it, actually, is worth.

          Note:



          --
          Ayaz Ahmed Khan

          Yours Forever in, | Webmaster,
          Cyberspace. | http://fast-ce.org/
          _______________ _______________ _______________ __
          I ditched Windows for Linux over two years ago.
          Life's never been better since.

          Comment

          • Ayaz Ahmed Khan

            #6
            Re: Variables as array size declarators.

            "Rolf Magnus" typed:
            [color=blue]
            > Ayaz Ahmed Khan wrote:
            >[color=green]
            >> Can a variable containing, say, an integer value be used as size
            >> declarator for an array of integers?[/color]
            >
            > No.
            >[color=green]
            >> g++ does not complain, but other compilers, such as Visual C++/6.0,
            >> Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I tested it
            >> on decline to use variables as size declarators.[/color]
            >
            > You probably didn't use g++ in ansi/iso mode. Try to use at least
            > the following command line switches:
            >
            > -ansi -pedantic -W -Wall[/color]


            Yes. It now complains when I use the above switches. But if I wasn't
            using g++ in ANSI/ISO mode before, why did it flag with an "ISO for
            scope" message, or something to that effect, an identifier declared
            in the for header that I tried to use elsewhere in the listing?


            --
            Ayaz Ahmed Khan

            Yours Forever in, | Webmaster,
            Cyberspace. | http://fast-ce.org/
            _______________ _______________ _______________ __
            I ditched Windows for Linux over two years ago.
            Life's never been better since.

            Comment

            • Rob Williscroft

              #7
              Re: Variables as array size declarators.

              Ayaz Ahmed Khan wrote in
              news:pan.2004.0 2.27.04.40.24.3 76057.7652@myre albox.com:
              [color=blue]
              > "Jakob Bieling" typed:
              >[color=green]
              >> "Ayaz Ahmed Khan" <resilient@myre albox.com> wrote in message
              >> news:pan.2004.0 2.26.15.51.50.7 45504.1849@myre albox.com...[color=darkred]
              >>> Can a variable containing, say, an integer value be used as size
              >>> declarator for an array of integers? g++ does not complain, but
              >>> other compilers, such as Visual C++/6.0, Borland Builder C++, Turbo
              >>> C++, and Dev-C++/4.0, that I tested it on decline to use variables
              >>> as size declarators.[/color]
              >>
              >>
              >> Not sure, but I think C allows this and g++ allows it as an
              >> extension.
              >> However, it is not part of Standard C++. If you want to use this
              >> kind of stuff, use the std::vector container class. It will do
              >> exactly what you want.[/color]
              >
              >
              > I had been under the impression that it was a feature incorporated
              > into standard C++. I found a reference to VLAs[1], but am not sure
              > what it, actually, is worth.
              >[/color]

              VLA's are a feature of Standard C, C++ compilers that support them
              (eg: g++) do so as an extension to Standard C++. IOW using VLA's in
              C++ is *not* portable.
              [color=blue]
              > Note:
              >
              > http://www.cray.com/craydoc/manuals/...-2179-003/z893
              > 434830malz.html
              >[/color]

              The page title says "Chapter 3. Cray C and C++ Extensions".


              Rob.
              --

              Comment

              • André Pönitz

                #8
                Re: Variables as array size declarators.

                In gnu.g++.help Ayaz Ahmed Khan <resilient@myre albox.com> wrote:[color=blue]
                > I had been under the impression that it was a feature incorporated
                > into standard C++.[/color]

                It's not in the current C++ Standard (1998) but in C (1999).
                I wouldn't use it in my code.

                Andre'

                Comment

                • Rolf Magnus

                  #9
                  Re: Variables as array size declarators.

                  Ayaz Ahmed Khan wrote:
                  [color=blue][color=green][color=darkred]
                  >>> g++ does not complain, but other compilers, such as Visual C++/6.0,
                  >>> Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I tested it
                  >>> on decline to use variables as size declarators.[/color]
                  >>
                  >> You probably didn't use g++ in ansi/iso mode. Try to use at least
                  >> the following command line switches:
                  >>
                  >> -ansi -pedantic -W -Wall[/color]
                  >
                  >
                  > Yes. It now complains when I use the above switches. But if I wasn't
                  > using g++ in ANSI/ISO mode before, why did it flag with an "ISO for
                  > scope" message, or something to that effect, an identifier declared
                  > in the for header that I tried to use elsewhere in the listing?[/color]

                  I think that g++ gives that error message when you try to use a variable
                  that was defined in a for loop after that loop. So e.g. the following
                  erroneous code would produce that:

                  #include <iostream>

                  int main()
                  {
                  for (int i = 0; i < 10; ++i)
                  std::cout << i << '\n';

                  i = 3; // error, i is not defined after the for loop
                  }

                  In some old C++ dialects before ISO, that was allowed, and i was defined
                  up until the end of the enclosing block. VC++ is known to still handle
                  this incorrectly.

                  Comment

                  • Ayaz Ahmed Khan

                    #10
                    Re: Variables as array size declarators.

                    "Rolf Magnus" typed:
                    [color=blue]
                    > Ayaz Ahmed Khan wrote:
                    >[color=green][color=darkred]
                    >>>> g++ does not complain, but other compilers, such as Visual
                    >>>> C++/6.0, Borland Builder C++, Turbo C++, and Dev-C++/4.0, that I
                    >>>> tested it on decline to use variables as size declarators.
                    >>>
                    >>> You probably didn't use g++ in ansi/iso mode. Try to use at least
                    >>> the following command line switches:
                    >>>
                    >>> -ansi -pedantic -W -Wall[/color]
                    >>
                    >>
                    >> Yes. It now complains when I use the above switches. But if I
                    >> wasn't using g++ in ANSI/ISO mode before, why did it flag with an
                    >> "ISO for scope" message, or something to that effect, an identifier
                    >> declared in the for header that I tried to use elsewhere in the
                    >> listing?[/color]
                    >
                    > I think that g++ gives that error message when you try to use a
                    > variable that was defined in a for loop after that loop. So e.g. the
                    > following erroneous code would produce that:
                    >
                    > #include <iostream>
                    >
                    > int main()
                    > {
                    > for (int i = 0; i < 10; ++i)
                    > std::cout << i << '\n';
                    >
                    > i = 3; // error, i is not defined after the for loop
                    > }
                    > }
                    > In some old C++ dialects before ISO, that was allowed, and i was
                    > defined up until the end of the enclosing block. VC++ is known to
                    > still handle this incorrectly.[/color]


                    Yes. Exactly that error. But the first time I came across it using
                    g++, I thought that g++ was, by default, running in ANSI/ISO
                    standards-complaint mode.

                    Anyway, thanks for your time.

                    --
                    Ayaz Ahmed Khan

                    Yours Forever in, | Webmaster,
                    Cyberspace. | http://fast-ce.org/
                    _______________ _______________ _______________ __
                    I ditched Windows for Linux over two years ago.
                    Life's never been better since.

                    Comment

                    • Ayaz Ahmed Khan

                      #11
                      Re: Variables as array size declarators.

                      "André Pönitz" typed:
                      [color=blue]
                      > In gnu.g++.help Ayaz Ahmed Khan <resilient@myre albox.com> wrote:[color=green]
                      >> I had been under the impression that it was a feature incorporated
                      >> into standard C++.[/color]
                      >
                      > It's not in the current C++ Standard (1998) but in C (1999). I
                      > wouldn't use it in my code.[/color]


                      Thanks, Andre.


                      --
                      Ayaz Ahmed Khan

                      Yours Forever in, | Webmaster,
                      Cyberspace. | http://fast-ce.org/
                      _______________ _______________ _______________ __
                      I ditched Windows for Linux over two years ago.
                      Life's never been better since.

                      Comment

                      • Ayaz Ahmed Khan

                        #12
                        Re: Variables as array size declarators.

                        "Rob Williscroft" typed:
                        [color=blue]
                        > Ayaz Ahmed Khan wrote in
                        > news:pan.2004.0 2.27.04.40.24.3 76057.7652@myre albox.com:
                        >[color=green]
                        >> "Jakob Bieling" typed:
                        >>[color=darkred]
                        >>> "Ayaz Ahmed Khan" <resilient@myre albox.com> wrote in message
                        >>> news:pan.2004.0 2.26.15.51.50.7 45504.1849@myre albox.com...
                        >>>> Can a variable containing, say, an integer value be used as size
                        >>>> declarator for an array of integers? g++ does not complain, but
                        >>>> other compilers, such as Visual C++/6.0, Borland Builder C++,
                        >>>> Turbo C++, and Dev-C++/4.0, that I tested it on decline to use
                        >>>> variables as size declarators.
                        >>>
                        >>>
                        >>> Not sure, but I think C allows this and g++ allows it as an
                        >>> extension.
                        >>> However, it is not part of Standard C++. If you want to use this
                        >>> kind of stuff, use the std::vector container class. It will do
                        >>> exactly what you want.[/color]
                        >>
                        >>
                        >> I had been under the impression that it was a feature incorporated
                        >> into standard C++. I found a reference to VLAs[1], but am not sure
                        >> what it, actually, is worth.
                        >>
                        >>[/color]
                        > VLA's are a feature of Standard C, C++ compilers that support them
                        > (eg: g++) do so as an extension to Standard C++. IOW using VLA's in
                        > C++ is *not* portable.[/color]


                        So I see. But aren't there advantages in using VLAs that are defined a
                        certain size, such as provided by the input variables, at run-time,
                        over other arrays for the same purposes that have to be specified a
                        size large enough to accomodate the input data, which can so much
                        as only be guessed anytime before run-time?


                        [color=blue][color=green]
                        >> Note:
                        >>
                        >> http://www.cray.com/craydoc/manuals/...-2179-003/z893
                        >> 434830malz.html
                        >>
                        >>[/color]
                        > The page title says "Chapter 3. Cray C and C++ Extensions".[/color]


                        Extensions. Perhaps, I didn't read it so thoroughly as I should have
                        had.


                        --
                        Ayaz Ahmed Khan

                        Yours Forever in, | Webmaster,
                        Cyberspace. | http://fast-ce.org/
                        _______________ _______________ _______________ __
                        I ditched Windows for Linux over two years ago.
                        Life's never been better since.

                        Comment

                        • Rob Williscroft

                          #13
                          Re: Variables as array size declarators.

                          Ayaz Ahmed Khan wrote in news:pan.2004.0 2.28.17.55.17.5 10698.1784
                          @myrealbox.com:
                          [color=blue][color=green]
                          >> VLA's are a feature of Standard C, C++ compilers that support them
                          >> (eg: g++) do so as an extension to Standard C++. IOW using VLA's in
                          >> C++ is *not* portable.[/color]
                          >
                          >
                          > So I see. But aren't there advantages in using VLAs that are defined a
                          > certain size, such as provided by the input variables, at run-time,
                          > over other arrays for the same purposes that have to be specified a
                          > size large enough to accomodate the input data, which can so much
                          > as only be guessed anytime before run-time?
                          >
                          >[/color]

                          Yes, but with C++ we have std::vector<> to solve that problem.

                          #include <vector>

                          extern void takes_a_char_bu ffer( char *buf, std::size_t sz );

                          int main()
                          {
                          std::size_t sz = /* some runtime value */;

                          std::vector< char > buf( sz );

                          takes_a_char_bu ffer( &buf[0], sz );
                          }

                          Rob.
                          --

                          Comment

                          • Ayaz Ahmed Khan

                            #14
                            Re: Variables as array size declarators.

                            "Rob Williscroft" typed:
                            [color=blue]
                            > Ayaz Ahmed Khan wrote in news:pan.2004.0 2.28.17.55.17.5 10698.1784
                            > @myrealbox.com:
                            >[color=green][color=darkred]
                            >>> VLA's are a feature of Standard C, C++ compilers that support them
                            >>> (eg: g++) do so as an extension to Standard C++. IOW using VLA's
                            >>> in C++ is *not* portable.[/color]
                            >>
                            >>
                            >> So I see. But aren't there advantages in using VLAs that are
                            >> defined a certain size, such as provided by the input variables, at
                            >> run-time, over other arrays for the same purposes that have to be
                            >> specified a size large enough to accomodate the input data, which
                            >> can so much as only be guessed anytime before run-time?
                            >>
                            >>
                            >>[/color]
                            > Yes, but with C++ we have std::vector<> to solve that problem.
                            >
                            > #include <vector>
                            >
                            > extern void takes_a_char_bu ffer( char *buf, std::size_t sz );
                            >
                            > int main()
                            > {
                            > std::size_t sz = /* some runtime value */;
                            >
                            > std::vector< char > buf( sz );
                            >
                            > takes_a_char_bu ffer( &buf[0], sz );
                            > }
                            > }
                            > Rob.[/color]


                            Thanks, Rob. I'm still far behind in studying containers and STL in
                            C++, having recently started learning "Classes and Objects".


                            --
                            Ayaz Ahmed Khan

                            Yours Forever in, | Webmaster,
                            Cyberspace. | http://fast-ce.org/
                            _______________ _______________
                            "To death, I shall hate thee."

                            Comment

                            Working...