vector <vector<short> throughs bad allocation

Nov 4, 2010 at 3:19pm
Hi guys.

I have a strange problem. I have a class (Foo) which have a variable which is defined like this:

vector <vector <short> > toTalk;

When I try to put something in that vector I get an error:

terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
/bin/sh: line 1: 26755 Aborted ./mem
Press Enter to continue!


So I tried to cheat. I created another variable in the function where I need to update toTalk and did the following:

1
2
3
4
5
6
7
8
9
vector < vector < short> >  tempTalk;
toTalk.resize(0);
while(//condition){
vector <short> temp;
...
tempTalk.push_back(temp);
}

this->toTalk = tempTalk;


But I get the same exception. When I remove the last line (//this->toTalk = tempTalk; ) then everything is fine (but it doesn't help me.

From what I read, bad alloc means that there was a problem assigning memory to taht variable. Why should that happen?

Thanks.

Yotam
Nov 4, 2010 at 4:45pm
operator new throws std::bad_alloc if memory allocation fails (ie, not enough memory).
Nov 4, 2010 at 5:45pm
I have found that part myself, but it doesn't make sense to me.

I know for a fact I have enough memory.
I'm not even at 10% of my memory usage. Further more, I have 125 members of Foo, each talking with 14 other. each vector <short> has 3 short number in it. Total of 30KB I don't think this should affect in any way.

Could it point on something else?

Yotam
Nov 4, 2010 at 5:50pm
vectors try to allocate contiguous memory. You might not have enough contiguous memory, so if it isn't needed, you could try a list or something. It could also be that your program has a hard limit on the amount of memory it can use.
Topic archived. No new replies allowed.