SU_Anonymous

Hi im facing a very strange problem: whenever i use vector push_back, then after like 20 push_backs, my program makes the system physical memory to shoot to around around 1.5 GB , execution gets so slow that it's almost dead and after some more time prog gets stuck at a point Below is the snippet where i think the prob shld be. can ne1 figure out the bug

void AND::buildObject (const string& gateInput1, const string& gateInput2, const string& gateOutput)
{
AND_Gate.reserve(500);
input1 = gateInput1;
input2 = gateInput2;
output = gateOutput;
AND_Gate.push_back(*this); // <------ here's the problem. If i comment out this line then //program runs just as normal. Even reserve service of vector is not helping
//AND_Gate is a vector which I have defined as: vector <AND> AND_Gate; and it contains the// objects of AND class
}


Re: Visual C++ Language typical problem with vector push_back

Marius Bancila

1.5GB of memory Because of 20 push_backs What are you puting in the vector What is AND and what is AND_gate






Re: Visual C++ Language typical problem with vector push_back

Stefan0073

You should consider using a deque instead of a vector. A vector must reserve the memory as a whole (continuous). If you're using a deque however, then the memory can be allocated in smaller blocks and you won't have as many memory problems as with a vector.




Re: Visual C++ Language typical problem with vector push_back

SU_Anonymous

Dear Marius,
I must have explained this in my starting thread itself neways.....AND_Gate is the vector:
vector <AND> AND_Gate; which stores the objects of class AND.




Re: Visual C++ Language typical problem with vector push_back

Holger Grund

While this may sound like a good idea in general, Visual C++ allocation strategy for std:Big Smileeque is somewhat bizarre.

-hg





Re: Visual C++ Language typical problem with vector push_back

prshaw

Is AND_Gate a member variable of AND

If so, then every time you call buildObject it will double in size, push_back() a copy of itself. After a few doublings I would expect you to run out of memory.

Phillip





Re: Visual C++ Language typical problem with vector push_back

SU_Anonymous

yeah i figured it out and probably that must had been the problem. Now it's working fine