1

スターター オブジェクトを削除する必要がある main() 関数の最後でクラッシュします。プログラムを実行すると、次のようなエラー メッセージが表示されます。デバッグ アサーションに失敗しました。式: _BLOCK_IS_VALID(pHead->nBlockUse)。スターター オブジェクトを削除するときにクラッシュしないようにするにはどうすればよいですか?

#include <iostream>
#include <fstream>
#include "olympic.h"

using namespace std;

ofstream csis;

int main() {
const int lanes = 4;
Ranker rank(lanes);

csis.open("csis.txt");

// First make a list of names and lane assignments.
Competitor* starters[lanes];

starters[0] = new Competitor("EmmyLou Harris", 1);
starters[1] = new Competitor("Nanci Griffith", 2);
starters[2] = new Competitor("Bonnie Raitt", 3);
starters[3] = new Competitor("Joni Mitchell", 4);

// The race is run; now assign a time to each person.
starters[0]->setTime((float)12.0);
starters[1]->setTime((float)12.8);
starters[2]->setTime((float)11.0);
starters[3]->setTime((float)10.3);

// Put everyone into the ranker.
for (int i = 0; i < lanes; i++)
    rank.addList(starters[i]);

// Now print out the list to make sure its right.
cout << "Competitors by lane are:" << endl;
csis << "Competitors by lane are:" << endl;
for (int i = 1; i <= lanes; i++)
    rank.getLane(i)->print();

// Finally, show how they finished.
cout << "Rankings by finish are:" << endl;
csis << "Rankings by finish are:" << endl;
for (int i = 1; i <= lanes; i++)
    rank.getFinish(i)->print();
for (int i = 0; i < lanes; i++)
    delete starters[i];

csis.close();

}

ランカー.cpp:

#include "ranker.h"
#include "competitor.h"
#include <stdlib.h>

Ranker::Ranker(int lanes) {
athlete = new Competitor*[lanes]; 
numAthletes = 0;
maxAthletes = lanes;
}

int Ranker::addList(Competitor* starter) {
if (numAthletes < maxAthletes && starter != NULL) {
    athlete[numAthletes] = starter;
    numAthletes++;

    return numAthletes;
}
else
    return 0;
}

Competitor* Ranker::getLane(int lane) {
for (int i = 0; i < numAthletes; i++) {
    if (athlete[i]->getLane() == lane) {
        return athlete[i];
    }
}
return NULL;
}


Competitor* Ranker::getFinish(int position) {
switch(position) {
    case 1:
        return athlete[3];
        break;
    case 2:
        return athlete[2];
        break;
    case 3:
        return athlete[1];
        break;
    case 4:
        return athlete[0];
        break;
}
return NULL;
}

int Ranker::getFilled() {
return numAthletes;
}

Ranker::~Ranker() {
delete [] athlete;
}

競合他社.h:

#ifndef _COMPETITOR_H
#define _COMPETITOR_H

 class Competitor {
     private:
        char* name;
        int lane;
        double time;
     public:
        Competitor(char* inputName, int inputLane);
        Competitor();
        void setTime(double inputTime);
        char* getName();
        int Competitor::getLane();
        double getTime();
        void print();
       ~Competitor();
    };

    #endif

競合他社.cpp:

#include "competitor.h"
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;


 Competitor::Competitor(char* inputName, int inputLane) {
name = inputName;
lane = inputLane;
}

Competitor::Competitor() {
name = 0;
lane = 0;
time = 0;
}

 void Competitor::setTime(double inputTime) {
time = inputTime;
}

char* Competitor::getName() {
return name;
}

int Competitor::getLane() {
return lane;
 }

 double Competitor::getTime() {
return time;
}

void Competitor::print() {
cout << setw(20) << name << setw(20) << lane << setw(20) << setprecision(4) << time        << endl;
}

Competitor::~Competitor() {
delete [] name;
}

コール スタック:

クラッシュ前: http://i.imgur.com/d4sKbKV.png クラッシュ後: http://i.imgur.com/C5cXth9.png

4

2 に答える 2

0

Competitor クラスを追加した後、Competitor のデストラクタでその名前を削除することが問題のようです。しかし、実際には削除できない文字列リテラルから割り当てます。アサーションにつながるスタックトレースがそれを証明すると確信しています。

この問題を解決する 1 つの方法は、std::string を使用して名前を格納することです。

于 2013-07-21T05:48:55.063 に答える
0

問題は、新しい char ではなく const char が割り当てられているデストラクタの char* 値を削除する場合です。そのため、コンストラクターを少し変更して、const char を新しい char にコピーしました。

Competitor::Competitor(char* inputName, int charlen, int inputLane) 
{
name = new char[charlen + 1];
memcpy(name , inputName, charlen );
name [charlen] = '\0'; 
lane = inputLane;
}
于 2014-07-09T06:48:53.537 に答える