can't figure out why there's a runtime error

this function is meant to merge two "hands" of cards by alternating both hands into the one hand. CardSet is a class and newSet is passed to the function as an instance of the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void CardSet::MergeShuffle(CardSet& newSet)
{//merges two sets of cards together, alternating between the sets
	int *Temp;
	int a = newSet.nCards, a2 = a + nCards;			//variables to store the size of Card from curret set and passed set

	Temp = new int[a2];					//pointer array big enough to store all of the cards

	for (int i = 0, x = 0, y = 0; i < a2; x++, y++) 	//alternate storing the cards
	{
		if (Card[x])					//if the card exists, store it
		{
			Temp[i] = Card[x];
			i++;
		}
		
		if (newSet.Card[y])				//if the card exists, store it
		{
			Temp[i] = newSet.Card[y];
			i++;
		}
	}	

	Card = new int[a2];

	for (int i = 0; i < a2; i++)				//copy the merged cards from Temp back into Card
		Card[i] = Temp[i];
}


if you have can improve it, please let me know, thanks guys.

the error is a malloc error
Last edited on
if (Card[x]) ¿are you sure that you can go all the way till `a2'?

1
2
3
	Card = new int[a2];
	for (int i = 0; i < a2; i++)
		Card[i] = Temp[i];
sigh
1
2
delete []Card;
Card = Temp;

By the way, you are leaking.
Topic archived. No new replies allowed.