Hi,
I have a large vector of structures (each contains 9 doubles, 1 int, 1 bool). When I "push_back" one more entry (almost 17,000,000'th), I get a bad_alloc exception. I am attaching a small script where I reproduced the problem
Please, pay attention to commented out pieces, they describe different ways of getting/not getting the problem: if I allocate the "size" elements right away in the vector declaration, and assign to each using operator=, I am fine; if I do "resize" right away for the whole size, I'm fine. If I try to either push_back (uncomment push_back line and comment out resizing and assignment in the loop), or resize element by element, I get the exception.Code:#include<iostream> #include<vector> using namespace std; struct InclEvent{ int incl_index; double p_e; double Q2; double W; bool tagged; // true/false tagged/untagged double p_spec; // double p_spec_raw; double cos_th_pq; double Wstar; double deltaz; double spec_theta; double spec_z; }; int main () { int get_res; InclEvent temp_evnt; int size=18000000; vector<InclEvent> Events; // Events.resize(size); for(uint ctr=0;ctr<size;ctr++){ Events.resize(ctr+1); cout<<"ctr "<<ctr<<endl; temp_evnt.incl_index=ctr; temp_evnt.p_e = 1.1; temp_evnt.Q2 = 1.2; temp_evnt.W = 1.3; temp_evnt.tagged = false; temp_evnt.p_spec = -10.0; temp_evnt.cos_th_pq=-10.0; temp_evnt.Wstar = -10.0; temp_evnt.deltaz= -10.0; Events.at(ctr)=temp_evnt; // Events.push_back(temp_evnt); } cin>>get_res; // delete Events; return 0; }
I am using g++: target i486-linux-gnu, gcc version 4.1.3.
Please, help to resolve this.