Hello,

I cannot figure out why my program crashes when it gets to the code below. Everything else in my coding works, except when I go to run this code...

void Auctioneer::matchingGame(){

   vector<Trader>::iterator it;

   int i=ask.size()-1;
   int x=0;
   int y=0;
   int bId;
   int tId;
   char h;
   int temp=0;
   int p;
   it=ask.end();   
     while(it != ask.begin()){  
            if(ask[i].getPrice()<=buy[i].getPrice()){//compare prices
              temp=(buy[i].getQuan()-ask[i].getQuan());
              h = buy[i].getType();
              bId = buy[i].getBidId();
              tId = buy[i].getTraderId();
              p = buy[i].getPrice();
                  if (temp <0){
                           temp = (ask[i].getQuan()-buy[i].getQuan());
                           h = ask[i].getType();
                           bId = ask[i].getBidId();
                           tId = ask[i].getTraderId();
                           p = ask[i].getPrice();
                           }
                 matchedAsk[x]=ask[i];
                 matchedBuy[x]=buy[i];          
                 ask.pop_back();
                 buy.pop_back();                                
                 if(h == 'A'){
                     ask[i].setUP(bId,tId,h,temp,p);
                     }else if(h=='B'){
                     buy[i].setUP(bId,tId,h,temp,p);
}
}else if (ask[i].getPrice()>buy[i].getPrice()){
      unmatchedBuy[y]=buy[i];
      buy.pop_back();
      }
--it;     
i--;
x++;
y++;
     }
    
int ab = ask.size()-1;
for (it=ask.end();it !=ask.begin();--it){
    unmatchedAsk[y]=ask[ab];
    ask.pop_back();
    ab--;
}

}

The above code is suppose to match bids then move them to the respective vector (matchedAsk and matchedBuy) by copying the elements to it then erasing the bid that was in the ask OR buy vector. For the bids that do not match, they are moved to the unmatchedAsk and/or unmatchedBuy vector then removed from the ask OR buy vector.


Any help will be appreciated.

Can you ballpark a region where it crashes? If you have a debugger see where it crashes or put in some cout statements in strategic spots to note the progress. Then I (or someone else) can take a look at that specific spot.

ok, I did what you asked. I debugged it and it is at line 28 it says: "An Access Violation (Segmentation Fault) raised in your program" then highlights:

matchedAsk[x]=ask[i];

do not know what it means, or how to correct it. This part of the code is suppose to copy the ask vectors elements at 'i' into matchedAsk at 'x'.

I have tried using:

copy(ask.end()-1,ask.end()-1,matchedAsk.end());

Which allows the program to run without crashing BUT does not actually copy that "row" of elements to matchedAsk (as I try outputting matchAsk elements and nothing turns up).

So what are the sizes of matchedAsk and ask in the line in your first reply? Check your values for i and x just before you execute the statement matchedAsk[x] = ask[i] .
I think in the second reply you have it going to one past the end of matchedAsk so that's not helping either(I'd have to go over the iterators but I'm not even sure one element is being copied there).

Can you post the full code here? Maybe we would be able to sort it out together when you do so..

Can you post how you have declared matchedAsk and ask vectors ?
Also towards the ending in the for[line 48] loop, I notice that you are not changing the value of the index y. Is that what you want ?

Well I solved my problem, I used the "insert" function instead. Thanks for your help people.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.