如何将指向对象的指针设置为 Null?C++

Hab*_*bit -1 c++ null pointers segmentation-fault

我正在开发一款 Roguelike 游戏,我将其设置为一个关卡上最多有 3 个怪物。所以我对怪物有3个指示。它们都是从 NULL 开始的。

怪物死后,我应该如何将指针设置回 NULL?或者也许我有错误的想法?本质上,我需要删除怪物,直到该指针重新生成为另一个怪物。

我已经尝试了一些方法,但我总是遇到段错误。

我尝试使用擦除、删除,只需将其设置为 NULL,然后重新指向它。想不通。

这是我设置指针的方法:

CreatureFactory myCreatures = CreatureFactory::instance();
    Creature * pCreature1 = NULL;
    Creature * pCreature2 = NULL;
    Creature * pCreature3 = NULL;;

    ItemFactory myItems = ItemFactory::instance();
    Item * pItem1 = myItems.generateItem();

    dl.placeInGame(personPlaying,randomGen);
    int monsterCount = 0;
    int turnCount = 0;
    bool playingGame = true;
    char playerController;

    while (playingGame){
            if ((turnCount % 2 == 0) && (personPlaying.getHP() < personPlay$
                    personPlaying.generateHP();
            }
            if (((turnCount % 3) == 0) && (monsterCount < 2)){
                    if(pCreature1 == NULL){
                            pCreature1 = myCreatures.generateCreature(4);
                            dl.placeInGame(pCreature1,randomGen);
                            monsterCount += 1;
                    }
                    else if(pCreature2 == NULL){
                            pCreature2 = myCreatures.generateCreature(4);
                            dl.placeInGame(pCreature2, randomGen);
                            monsterCount += 1;
                    }
                    else if(pCreature3 == NULL){
                            pCreature3 = myCreatures.generateCreature(4);
                            monsterCount += 1;
                    }
            }
            dl.dump();
            personPlaying.dumpStats();
Run Code Online (Sandbox Code Playgroud)
 CreatureFactory code..
 CreatureFactory & CreatureFactory::instance(){
    static CreatureFactory myObj;
    return myObj;
}

CreatureFactory::CreatureFactory(){
    m_mtRandom.seed( time(NULL) );
    fstream xmlFile;
    xmlFile.open("critters.xml");
    vector<XMLSerializable*> pObjects;
    parseXML(xmlFile, pObjects);

    XMLSerializable * pObject;

    for(auto it = pObjects.begin(); it != pObjects.end(); it++){
            pObject = (*it);
            Creature * pCreature = dynamic_cast<Creature*>(pObject);
            if (pCreature != NULL){
                    m_vCreatures.push_back(pCreature);
            }
    }
}

CreatureFactory::~CreatureFactory(){

}
    Creature * CreatureFactory::generateCreature(int iMaxLevel) {
    vector<Creature*> tempCreatures;

    for(auto it = m_vCreatures.begin(); it != m_vCreatures.end(); it++){
            if ((*it)->getLevel() <= iMaxLevel){
                    tempCreatures.push_back((*it));
            }
    }
    int randomCreature = (m_mtRandom() % (tempCreatures.size() - 1));


    Creature * pCreature = tempCreatures.at(randomCreature);

    Creature * pReturnValue = new Creature(*pCreature);

    return pReturnValue;
}
Run Code Online (Sandbox Code Playgroud)

然后我进行了大量的循环并使用了大量的类。指向这些怪物对象的指针通过大约 6 或 7 个类传递。当其中一个类的 HP 为 0 时,我需要将其设置为 NULL。

谢谢

Hol*_*sch 5

在c++中,你不应该将指针设置为null,因为对象仍然存在于内存中,但没有任何对其的引用->内存泄漏。相反,您应该删除该对象:

delete pCreatureN;
Run Code Online (Sandbox Code Playgroud)

这基本上将从内存中删除该对象并使指针成为空指针。之后就pCreatureN = NULL;可以了。

  • 删除*不会*将指针设置为空! (7认同)