-1

I'm working on a roguelike game and I have it set up so that at MOST there will be 3 monsters on a level. So I have 3 pointers to monsters. They all start out as NULL.

How should I go about setting the pointer back to NULL after that monster dies? Or maybe I have the wrong idea? Essentially I need the monster to be removed until that pointer is regenerated as another monster.

I've tried a few things but I just keep getting seg faults.

I tried using erase, delete, simply setting it to NULL, and repointing it. Can't figure it out.

Here's how I set the pointers:

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();

 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;
}

Then I do a ton of looping and use tons of classes. The pointers to these monster objects get passed through about 6 or 7 classes.. one of which I need to set to NULL when its HP hits 0.

Thanks

Habit
  • 839
  • 3
  • 9
  • 21
  • Please show us some code. Also, please tag this question with the appropriate language tag (e.g. `[c]` or `[c++]`). – NPE Apr 24 '13 at 05:50
  • Why.. It's a general question. Yeah sorry I forgot to tag c++ I'll update it. – Habit Apr 24 '13 at 05:53
  • It depends on whether the pointers "own" the monsters, i.e. are they dynamically allocated and must they be destroyed? – juanchopanza Apr 24 '13 at 05:56
  • No they don't own the monster and they're not dynamically allocated. They shouldn't be destroyed. I just need them to be blank and ready to point to another object – Habit Apr 24 '13 at 06:00
  • What is wrong with `monster1 = NULL;`? Your question seems very basic, so I guess there's a bit more to it. I think you seg faults are being caused by something else entirely. This is an XY problem. As usual, show us some code. – john Apr 24 '13 at 06:02
  • The code is spread out through 10+ files and a couple thousand lines. – Habit Apr 24 '13 at 06:07
  • OK I'm guessing that the problem is that you set the pointer to NULL in one part of your program but there are *copies* of that pointer in other parts of the program that which don't get set to NULL. Is that right? There's nothing magic about pointers, just because you set a pointer to NULL in one place, doesn't mean that every identical pointer throughout your code gets set to NULL as well. – john Apr 24 '13 at 06:09
  • @Justin Pick out the relevant code and put it in your question. A few lines where you create the monster. a few lines where you delete it, and maybe a few lines of where you're getting the segfault. You don't have to include the entire source code. – Captain Obvlious Apr 24 '13 at 06:09

1 Answers1

5

In c++ you should not set the pointer to null, since the Object will still exists in memory, but without any reference to it-> memory leak. Instead you should delete the Object:

delete pCreatureN;

This will basically remove the object from memory and makes the pointer being a null pointer. After that pCreatureN = NULL; is ok.