We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

adding element variable in dynamic memory

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;
}
3
Contributors
3
Replies
2 Hours
Discussion Span
9 Months Ago
Last Updated
5
Views
pkfx
Newbie Poster
7 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

thank you

pkfx
Newbie Poster
7 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Ancient Dragon
Achieved Level 70
Team Colleague
32,274 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0648 seconds using 2.72MB