0

I am getting a run time error when my program hits this code. I am sure the delete is causing it, I just don't see what is going wrong.

Here is the function: (FavShows is a class defined earlier)

void classInit()
{
    int numOfRecs;
    cout << "How many records will you enter?" << endl;
    cin >> numOfRecs;
    FavShows *m = new FavShows[numOfRecs];

    for( int i = 0; i < numOfRecs; i++)
    {
        m[i].initRec();
    }

for( int i = 0; i < numOfRecs; i++)
{
    m[i].printRec();
}
delete m;
}

The error I receive states: the error is in dbgdel.cpp ( i assume this is a memeber of one of the libs) Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

1
  • 2
    should call delete [] m Commented Dec 8, 2013 at 5:29

1 Answer 1

3

Simply use std::vector, avoid the pain.


For the code as given, note a new[] needs a delete[], not plain single-object delete.


With a std::vector it's much easier to just count the records entered by the user, instead of asking up-front how many. For each record inputted, use push_back to add it to the end of the vector. Then starting with an empty vector.

Sign up to request clarification or add additional context in comments.

2 Comments

FACEPALM, Thanks, that fixed it, I must be getting tired!
I will give it a try, we havnt covered vectors yet, but our final will give extra credit if we utilize a vector. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.