Defining and initializing arrays

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

    #1

    Defining and initializing arrays

    Is this a valid C++ program that will not crash on any machine?

    #include <iostream>
    using namespace std;
    int main( void ) {
    int i;
    cin >i;
    double X[i];

    X[i-1] = 1123;
    cout << X[i-1] << endl;

    return 0;
    }

    What does the standard say about defining and initializing arrays of
    length
    non-const? Is it illegal? Or is that legal in C++?

    Thanks,
    --j

  • Frederick Gotham

    #2
    Re: Defining and initializing arrays

    John posted:
    What does the standard say about defining and initializing arrays of
    length
    non-const? Is it illegal? Or is that legal in C++?
    According to the current C++ Standard, the length of an array must be a
    compile-time constant.

    Many compilers provide an extension whereby the programmer can define an
    array using a runtime value.

    If you're writing portable code, use "new" or "vector" or "malloc" instead.

    --

    Frederick Gotham

    Comment

    • S S

      #3
      Re: Defining and initializing arrays


      John wrote:
      Is this a valid C++ program that will not crash on any machine?
      >
      #include <iostream>
      using namespace std;
      int main( void ) {
      int i;
      cin >i;
      double X[i];
      >
      X[i-1] = 1123;
      cout << X[i-1] << endl;
      >
      return 0;
      }
      >
      What does the standard say about defining and initializing arrays of
      length
      non-const? Is it illegal? Or is that legal in C++?
      >
      Did you try it even on one machine before posting?
      Thanks,
      --j

      Comment

      • Frederick Gotham

        #4
        Re: Defining and initializing arrays

        S S posted:
        Did you try it even on one machine before posting?

        Most compilers will compile it no problem -- if they're not in Standard C++
        mode, that is.

        --

        Frederick Gotham

        Comment

        • Rolf Magnus

          #5
          Re: Defining and initializing arrays

          John wrote:
          What does the standard say about defining and initializing arrays of
          length non-const? Is it illegal? Or is that legal in C++?
          It would be legal in C, but it's illegal in C++.

          Comment

          • Howard

            #6
            Re: Defining and initializing arrays


            "John" <weekender_ny@y ahoo.comwrote in message
            news:1158773065 .926451.251180@ m7g2000cwm.goog legroups.com...
            Is this a valid C++ program that will not crash on any machine?
            >
            Well, I've seen a few machines that had a tendency to crash, regardless of
            what you ran on them. :-) Of course, that's not what you're really
            asking...
            #include <iostream>
            using namespace std;
            int main( void ) {
            int i;
            cin >i;
            double X[i];
            >
            X[i-1] = 1123;
            cout << X[i-1] << endl;
            >
            return 0;
            }
            >
            What does the standard say about defining and initializing arrays of
            length
            non-const? Is it illegal? Or is that legal in C++?
            The standard says that the array size has to be a compile-time constant.
            The above code should not compile (and if it does, then that's a compiler
            extension, and is non-standard).

            If you need to specify the size at run-time, you can define X as a double*,
            and dynamically allocate using new[] (and later delete[] to clean up).
            Better still, use std::vector!

            -Howard


            Comment

            • amparikh@gmail.com

              #7
              Re: Defining and initializing arrays


              John wrote:
              Is this a valid C++ program that will not crash on any machine?
              >
              #include <iostream>
              using namespace std;
              int main( void ) {
              int i;
              cin >i;
              double X[i];
              >
              X[i-1] = 1123;
              cout << X[i-1] << endl;
              >
              return 0;
              }
              >
              What does the standard say about defining and initializing arrays of
              length
              non-const? Is it illegal? Or is that legal in C++?
              >
              Thanks,
              --j
              It needs to be a constant. Think about it for a second. How can the
              compiler possibly guess as to how much memory it needs to reserve ?

              Comment

              • Frederick Gotham

                #8
                Re: Defining and initializing arrays

                Rolf Magnus posted:
                John wrote:
                >
                >What does the standard say about defining and initializing arrays of
                >length non-const? Is it illegal? Or is that legal in C++?
                >
                It would be legal in C, but it's illegal in C++.

                Only in C99, which has yet to take the place of C89.

                --

                Frederick Gotham

                Comment

                • Gianni Mariani

                  #9
                  Re: Defining and initializing arrays

                  amparikh@gmail. com wrote:
                  ....
                  It needs to be a constant. Think about it for a second. How can the
                  compiler possibly guess as to how much memory it needs to reserve ?
                  >
                  Some compilers actually implement that feature as an extension so your
                  question obviously has an answer. The C99 standard actually allows this
                  and it is one way in which an array can be dynamically allocated.

                  Comment

                  • twomers

                    #10
                    Re: Defining and initializing arrays

                    If you need to specify the size at run-time, you can define X as a double*,
                    and dynamically allocate using new[] (and later delete[] to clean up).
                    Better still, use std::vector!
                    >
                    -Howard
                    Don't mix up new and delete syntax!



                    int num;
                    cout<< "Enter a number: ";
                    cin >num;

                    int *i = new int[num];

                    // do something

                    // if the pointer i was not an dynamically allocated array, you can use
                    this
                    // delete i;

                    // however it IS an array (probably), so you have to do this
                    delete []i;



                    Just thought I'd clarify in case the OP didn't know about the syntax,
                    odd as it is.

                    Comment

                    • Rolf Magnus

                      #11
                      Re: Defining and initializing arrays

                      Frederick Gotham wrote:
                      Rolf Magnus posted:
                      >
                      >John wrote:
                      >>
                      >>What does the standard say about defining and initializing arrays of
                      >>length non-const? Is it illegal? Or is that legal in C++?
                      >>
                      >It would be legal in C, but it's illegal in C++.
                      >
                      >
                      Only in C99, which has yet to take the place of C89.
                      It did when it was released. There is no C89 anymore. In standard speak:

                      "This second edition cancels and replaces the first edition, ISO/IEC
                      9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC
                      9899/AMD1:1995, and ISO/IEC 9899/COR2:1996."

                      It's a shame that so many compilers still don't even try to be standard
                      compliant, considering that C89 ceased to be a standard 7 years ago. When I
                      talk about "C" without any further notice, I talk about C99.

                      Comment

                      • Howard

                        #12
                        Re: Defining and initializing arrays


                        "twomers" <twomers@gmail. comwrote in message
                        news:1158797290 .233112.276480@ m7g2000cwm.goog legroups.com...
                        >If you need to specify the size at run-time, you can define X as a
                        >double*,
                        >and dynamically allocate using new[] (and later delete[] to clean up).
                        >Better still, use std::vector!
                        >>
                        >-Howard
                        >
                        Don't mix up new and delete syntax!
                        Huh? I didn't!
                        >
                        int num;
                        cout<< "Enter a number: ";
                        cin >num;
                        >
                        int *i = new int[num];
                        >
                        // do something
                        >
                        // if the pointer i was not an dynamically allocated array, you can use
                        this
                        // delete i;
                        >
                        // however it IS an array (probably), so you have to do this
                        delete []i;
                        >
                        Probably? Looks like an array to me.
                        >
                        Just thought I'd clarify in case the OP didn't know about the syntax,
                        odd as it is.
                        >
                        Seems more confusing to me, now. I explicitly described using new[] and
                        delete[]. Why introduce the other syntax?

                        -Howard



                        Comment

                        • Frederick Gotham

                          #13
                          Re: Defining and initializing arrays

                          Rolf Magnus posted:
                          >Only in C99, which has yet to take the place of C89.
                          >
                          It did when it was released. There is no C89 anymore. In standard speak:
                          >
                          "This second edition cancels and replaces the first edition, ISO/IEC
                          9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC
                          9899/AMD1:1995, and ISO/IEC 9899/COR2:1996."

                          Ask any decent C programmer which Standard they're working off.

                          It's a shame that so many compilers still don't even try to be standard
                          compliant, considering that C89 ceased to be a standard 7 years ago.

                          It is very much still a Standard and has a great many followers.

                          When I talk about "C" without any further notice, I talk about C99.

                          Over on comp.lang.c, C refers to C89.

                          --

                          Frederick Gotham

                          Comment

                          • Jerry Coffin

                            #14
                            Re: Defining and initializing arrays

                            In article <1158773065.926 451.251180@m7g2 000cwm.googlegr oups.com>,
                            weekender_ny@ya hoo.com says...

                            [ ... ]
                            What does the standard say about defining and initializing arrays of
                            length non-const? Is it illegal? Or is that legal in C++?
                            It is not allowed in C++. Rather than directly using new[] and delete[]
                            as has been mentioned elsethread, you normally want to use std::vector:

                            #include <iostream>
                            #include <vector>

                            int main() {
                            int i;
                            cin >i;
                            std::vector<dou blex(i);
                            x[i-1] = 1123;

                            std::cout << x[i-1] << std::endl;
                            return 0;
                            }

                            --
                            Later,
                            Jerry.

                            The universe is a figment of its own imagination.

                            Comment

                            Working...