Difference in the array declaration

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

    Difference in the array declaration

    Hi,

    Is it possible to answer the following question: what is the difference
    between two declarations:

    vector<double> a(n);

    and

    double a[n];

    When I put, say, n=1000000, the program with the first line is working well,
    whereas with the second it terminates with error. What is the maximum
    possible array that can be defined this way? For n=100000 both declarations
    works fine.
    Compiler g++.

    Thank you.

    Arthur


  • Marcin Vorbrodt

    #2
    Re: Difference in the array declaration

    "Arthur Sinko" <asinko@msn.com > wrote in message
    news:7s9ab.1834 1$x21.3339@twis ter.southeast.r r.com...[color=blue]
    > Hi,
    >
    > Is it possible to answer the following question: what is the difference
    > between two declarations:
    >
    > vector<double> a(n);
    >
    > and
    >
    > double a[n];
    >
    > When I put, say, n=1000000, the program with the first line is working[/color]
    well,[color=blue]
    > whereas with the second it terminates with error. What is the maximum
    > possible array that can be defined this way? For n=100000 both[/color]
    declarations[color=blue]
    > works fine.
    > Compiler g++.
    >
    > Thank you.
    >
    > Arthur
    >
    >[/color]

    double a[1000000]; is allocated on the stack, which probably causes the
    stack to overflow. maximum size, dont know, depends on the size of the
    stack, several Ks, Megs maybe???

    vector<double> a(1000000); is also allocated on the stack... but the array
    it used internally is allocated on the heap (free store). in other works,
    vector's internal variables (like current size, capacity, etc) are on the
    stack, together with a pointer to the actual data (which is stored on the
    heap). heap can hold far more data than the stack can. maximum size... Total
    RAM - Allready Allocated RAM :-)

    hope that helps.

    Martin


    Comment

    • Arthur Sinko

      #3
      Re: Difference in the array declaration

      Thank you very much.

      Arthur

      "Marcin Vorbrodt" <mvorbro@eos.nc su.edu> wrote in message
      news:bkb911$iie $1@uni00nw.unit y.ncsu.edu...[color=blue]
      > "Arthur Sinko" <asinko@msn.com > wrote in message
      > news:7s9ab.1834 1$x21.3339@twis ter.southeast.r r.com...[color=green]
      > > Hi,
      > >
      > > Is it possible to answer the following question: what is the difference
      > > between two declarations:
      > >
      > > vector<double> a(n);
      > >
      > > and
      > >
      > > double a[n];
      > >
      > > When I put, say, n=1000000, the program with the first line is working[/color]
      > well,[color=green]
      > > whereas with the second it terminates with error. What is the maximum
      > > possible array that can be defined this way? For n=100000 both[/color]
      > declarations[color=green]
      > > works fine.
      > > Compiler g++.
      > >
      > > Thank you.
      > >
      > > Arthur
      > >
      > >[/color]
      >
      > double a[1000000]; is allocated on the stack, which probably causes the
      > stack to overflow. maximum size, dont know, depends on the size of the
      > stack, several Ks, Megs maybe???
      >
      > vector<double> a(1000000); is also allocated on the stack... but the array
      > it used internally is allocated on the heap (free store). in other works,
      > vector's internal variables (like current size, capacity, etc) are on the
      > stack, together with a pointer to the actual data (which is stored on the
      > heap). heap can hold far more data than the stack can. maximum size...[/color]
      Total[color=blue]
      > RAM - Allready Allocated RAM :-)
      >
      > hope that helps.
      >
      > Martin
      >
      >[/color]


      Comment

      • Ron Natalie

        #4
        Re: Difference in the array declaration


        "Arthur Sinko" <asinko@msn.com > wrote in message news:7s9ab.1834 1$x21.3339@twis ter.southeast.r r.com...[color=blue]
        > Hi,
        >
        > Is it possible to answer the following question: what is the difference
        > between two declarations:
        >
        > vector<double> a(n);
        >
        > and
        >
        > double a[n];
        >
        > When I put, say, n=1000000, the program with the first line is working well,
        > whereas with the second it terminates with error. What is the maximum
        > possible array that can be defined this way? For n=100000 both declarations
        > works fine.[/color]

        vector internally calls an allocator which by default just invokes new for the objets.
        While the vector itself is an auto variable, it really only typically has a few pointers
        inside it. The memory is dynamically allocated elsewhere.

        The array tries to create the array wherever auto storage is kept ( usually on a runtime
        stack). Auto variables typically have smaller maximum sizes than dynamic ones.


        Comment

        • J. Campbell

          #5
          Re: Difference in the array declaration

          "Arthur Sinko" <asinko@msn.com > wrote in message news:<7s9ab.183 41$x21.3339@twi ster.southeast. rr.com>...[color=blue]
          > Hi,
          >
          > Is it possible to answer the following question: what is the difference
          > between two declarations:
          >
          > vector<double> a(n);
          >
          > and
          >
          > double a[n];
          >
          > When I put, say, n=1000000, the program with the first line is working well,
          > whereas with the second it terminates with error. What is the maximum
          > possible array that can be defined this way? For n=100000 both declarations
          > works fine.
          > Compiler g++.
          >
          > Thank you.
          >
          > Arthur[/color]

          to place a 1000000 element array on the heap do it like:

          int n = 1000000;
          double* a;
          a = new double [n];

          Comment

          • Kevin Goodsell

            #6
            Re: Difference in the array declaration

            J. Campbell wrote:[color=blue]
            >
            > to place a 1000000 element array on the heap do it like:
            >
            > int n = 1000000;
            > double* a;
            > a = new double [n];[/color]

            Why like that rather than like this:

            std::vector<dou ble> a(n);

            ?

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • J. Campbell

              #7
              Re: Difference in the array declaration

              Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote in message news:<Qeoab.852 5$BS5.3069@news read4.news.pas. earthlink.net>. ..[color=blue]
              > J. Campbell wrote:[color=green]
              > >
              > > to place a 1000000 element array on the heap do it like:
              > >
              > > int n = 1000000;
              > > double* a;
              > > a = new double [n];[/color]
              >
              > Why like that rather than like this:
              >
              > std::vector<dou ble> a(n);
              >
              > ?
              >
              > -Kevin[/color]

              well...the OP knows how to use vectors and arrays...he simply wanted
              to know why his array ran out of space, while the vector didn't, so I
              was showing how to put the array on the heap.

              Comment

              • Kevin Goodsell

                #8
                Re: Difference in the array declaration

                J. Campbell wrote:[color=blue]
                > Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote in message news:<Qeoab.852 5$BS5.3069@news read4.news.pas. earthlink.net>. ..
                >[color=green]
                >>
                >>Why like that rather than like this:
                >>
                >>std::vector<d ouble> a(n);
                >>
                >>?
                >>
                >>-Kevin[/color]
                >
                >
                > well...the OP knows how to use vectors and arrays...he simply wanted
                > to know why his array ran out of space, while the vector didn't, so I
                > was showing how to put the array on the heap.[/color]

                It's worth noting that using a std::vector has a number of advantages
                over managing your own dynamically allocated array, even if you don't
                plan on doing resizing:

                1) You don't need to worry about delete[]ing it, thus preventing memory
                leaks.

                2) You don't need to worry about delete[]ing it, thus preventing the
                possibility of undefined behavior if you make the all-too-common mistake
                of using delete instead of delete[].

                3) The size is stored automatically and can be retrieved using the
                ..size() function - you don't need to store it in a separate variable.

                4) When invoking a standard algorithm, you can use the nice, clean
                (vec.begin(), vec.end()) syntax for the sequence.

                5) Exception safety - if an exception is thrown, the vector's destructor
                will free its memory and destruct the objects it held. With an array
                you'd have to either use a smart pointer (and you can't use auto_ptr for
                this), or catch the exception, delete[] the array, and re-throw.

                6) You have the option of checked indexing using the .at() member.

                (And probably more)

                -Kevin
                --
                My email address is valid, but changes periodically.
                To contact me please use the address from a recent posting.

                Comment

                Working...