vector : reserve(LONG_MAX)

A

Alex Vinokur

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)

===============================
 
P

Peter van Merkerk

Alex Vinokur said:
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.
 
P

Pete Becker

Alex said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top