Advertisement

05.13.2008 at 07:39AM PDT, ID: 23397964
[x]
Attachment Details

How to store more then max_size objects on a vector, which is size_t

Asked by tillo-httpdnet in C++ Programming Language, Physics & Artificial Intelligence in Game Programming, Miscellaneous Programming

Tags: C++, terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted, libstd++

I must store a very large number of very small objects on a vector.

It seems like libstd++ (like any other standard C++ library)'s STL vector header defines max_size() as size_t.

Can I heritate the vector class and avoid this limit (having only the physical memory limit after that)? If yes, how can I do it? Is there another solution?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
/*
 * My code snippet (code.cpp)
 */
    for(item = 0; item < m_NumSetShipsAtt.size(); item++)
	{
        // create attacking objects; for every ship 1 object
		obj = FillObj(m_NumSetShipsAtt[item].Type, ATTER, m_NumSetShipsAtt[item].OwnerID);
        if(m_NumSetShipsAtt[item].OwnerID != CurrPl)
            CurrPl = m_NumSetShipsAtt[item].OwnerID;
 
        for(size_t o = 0; o < m_NumSetShipsAtt[item].Num; o++) {
        	try {
				m_AttObj->push_back(obj);
        	}
        	catch (bad_alloc& xa) {
 
        	    cerr << "In catch block, so an exception occurred: " << xa.what() << endl;
        	    cerr << "ships added so far: " << o << endl;
        	    cerr << "m_AttObj's size is: " << m_AttObj->size() << endl;
        	    cerr << "m_AttObj's capacity is: " << m_AttObj->capacity() << endl;
        	    cerr << "m_AttObj's max size is: " << m_AttObj->max_size() << endl << endl;
        	    cerr << "Exiting...";
        	    return 1;
        	}
        }
	}
 
/*
 * m_AttObj declaration snippet (code.h)
 */
	vector<Obj>* m_AttObj, *m_DefObj;
 
/*
 * vector declaration snippets (libstd++'s stl_vector.h)
 */
  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
    class vector : protected _Vector_base<_Tp, _Alloc>
    {
// ... much more
      /**  Returns the size() of the largest possible %vector.  */
      size_type
      max_size() const
      { return _M_get_Tp_allocator().max_size(); }
// ... much more again
 
Loading Advertisement...
 
[+][-]05.13.2008 at 07:43AM PDT, ID: 21555528

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 08:01AM PDT, ID: 21555717

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C++ Programming Language, Physics & Artificial Intelligence in Game Programming, Miscellaneous Programming
Tags: C++, terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted, libstd++
Sign Up Now!
Solution Provided By: jkr
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.13.2008 at 08:14AM PDT, ID: 21555859

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 08:18AM PDT, ID: 21555907

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 08:22AM PDT, ID: 21555946

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 04:20PM PDT, ID: 21560105

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 10:15PM PDT, ID: 21561434

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628