vector : reserve(LONG_MAX)

Discussion in 'C++' started by Alex Vinokur, Apr 22, 2004.

  1. Alex Vinokur

    Alex Vinokur Guest

    Method vector.reserve() returns no value.
    How can we prevent the following situation?

    ====== foo.cpp ======
    #include <climits>
    #include <vector>
    #include <iostream>
    using namespace std;

    int main()
    {
    vector<int> v (100e);
    cout << v.capacity() << endl;
    v.reserve(LONG_MAX);
    cout << v.capacity() << endl;
    return 0;
    }
    =====================

    ====== Compilation & Run ======

    $ g++ -v
    [---omitted---]
    gcc version 3.3.1 (cygming special)

    $ g++ -W -Wall foo.cpp

    $ a
    100
    Aborted (core dumped)

    ===============================

    --
    Alex Vinokur
    mailto:
    http://mathforum.org/library/view/10978.html
     
    Alex Vinokur, Apr 22, 2004
    #1
    1. Advertising

  2. "Alex Vinokur" <> wrote in message
    news:c68keg$9b16c$-berlin.de...
    >
    > Method vector.reserve() returns no value.
    > How can we prevent the following situation?
    >
    > ====== foo.cpp ======
    > #include <climits>
    > #include <vector>
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > vector<int> v (100e);
    > cout << v.capacity() << endl;
    > v.reserve(LONG_MAX);
    > cout << v.capacity() << endl;
    > return 0;
    > }
    > =====================
    >
    > ====== Compilation & Run ======
    >
    > $ g++ -v
    > [---omitted---]
    > gcc version 3.3.1 (cygming special)
    >
    > $ g++ -W -Wall foo.cpp
    >
    > $ a
    > 100
    > Aborted (core dumped)


    If LONG_MAX > std::vector::max_size() with your standard library
    implementation it should throw a std::length_error exception.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Apr 22, 2004
    #2
    1. Advertising

  3. Alex Vinokur

    Pete Becker Guest

    Alex Vinokur wrote:
    >
    > Method vector.reserve() returns no value.
    > How can we prevent the following situation?
    >
    > ====== foo.cpp ======
    > #include <climits>
    > #include <vector>
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > vector<int> v (100e);
    > cout << v.capacity() << endl;
    > v.reserve(LONG_MAX);
    > cout << v.capacity() << endl;
    > return 0;
    > }
    > =====================
    >
    > ====== Compilation & Run ======
    >
    > $ g++ -v
    > [---omitted---]
    > gcc version 3.3.1 (cygming special)
    >
    > $ g++ -W -Wall foo.cpp
    >
    > $ a
    > 100
    > Aborted (core dumped)
    >
    > ===============================
    >


    Catch the exception.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Apr 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john smith

    vector.reserve

    john smith, Jul 25, 2003, in forum: C++
    Replies:
    5
    Views:
    713
    John Harrison
    Jul 25, 2003
  2. Matthias Kaeppler

    seg-fault on vector-auto-reserve

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    2
    Views:
    485
    Victor Bazarov
    Feb 27, 2005
  3. Gary Kuehn
    Replies:
    2
    Views:
    495
    Gary Kuehn
    Jul 19, 2005
  4. Alex Vinokur
    Replies:
    8
    Views:
    409
    Alex Vinokur
    Sep 23, 2005
  5. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
Loading...

Share This Page