Im trying to increase a dynamic array by 1 element and assign a integer variable to it but recieve
a heap overload error.
Any advice would be appreciated.

void CardSet::AddCard(int cardNum)
{
    int* tmp; 
    nCards++;

    tmp = new int[nCards];

    for(int i = 0; i < (nCards - 1); i++)
    {
        tmp[i] = Card[i];
    }

    delete [] Card;
    Card = tmp;
    Card[nCards] = cardNum;
}

Recommended Answers

All 3 Replies

Card[nCards] is one beyond the array definition. Since you allocated nCards integers, your last available value is Card[nCards-1].

thank you

maybe line 6 should have been this: tmp = new int[nCards+1];

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.