c++ - Multiple uses of new and delete -


i'm simulating traditional card game between 2 players using c++: defined "card" structure that

card sample; cout << sample.numb << " of " << sample.suit << " ownded player " << sample.own 

reads e.g. "10 of spades owned player 2".

i set vector "deck" (of size 40) keeping track of every card, , i'm dynamically allocating vectors "g1" "g2" , "tab" @ every step of game, in order handle player 1's hand, player 2's hand , cards on table 3 separated objects (this kind of necessary game itself).

since players keep putting cards on table , gaining/losing new cards sizes keep changing, every time this:

delete g1; delete g2; delete tab; n1=count (1,deck);  // counts how many cards in "deck" have .own field==1 n2=count (2,deck);  // counts how many cards in "deck" have .own field==2 nt=count (0,deck);  // cards having .own field == 0 on table g1 = new card [n1]; g2 = new card [n2]; tab = new card [nt]; k=0;               // using move through vectors re-allocated (i=0;i<40;i++) {   if(deck[i].own==1) {      g1[k]=deck[i];      k++;   } } // fill "g2" , "tab" pretty in same way 

the script compiles , runs, after couple of rounds (carried out correctly) "invalid next size (fast)" , segfault. reading around found out happens when try delete twice or likes of it. script doesn't delete twice, suspect such frequent use of new-delete-new deprecated , might cause of problem.

thoughts?

edit solved issue using std::vector, suggest doing same ever having same problem; answered.

peace,

tom

you'd better use array deletion g1, g2 , tab: eg: delete [] g1;

of course, you're far better off using std::vector , not handling memory management directly yourself.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -