CavemanAGz
CavemanAGz

Reputation: 43

Error when deleting dynamic array c++

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)

Upvotes: 0

Views: 241

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

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.

Upvotes: 3

Related Questions