0

I am trying to create a null terminated array of objects like this

void Game::createCreatures(int numCreatures) {
    creatures = new Creature *[numCreatures + 1];
    for (int i = 0; i <= numCreatures; i++) {
        if(i < numCreatures) {
            creatures[i] = new Runner(maze);
        } else creatures[i] = NULL;
    }
}

Then access them like this

for (Creature *creature = creatures[0]; creature != NULL; creature++) {
    creature->travel();
}

What exactly am I doing wrong? I am receiving a EXC_BAD_ACCESS when I attempt to 'travel' the creature. I know there is something wrong with the creation of the array because if I attempt to print the address of all of the creatures using my accessing for loop, it prints forever. I know there is something wrong with my pointer logic, help?

creatures declaration is this

Creature **creatures;
8
  • 2
    If this isn't an assignment, seriously, consider std::vector and std::unique_ptr :V. Commented Nov 15, 2014 at 0:32
  • 1
    @Sneftel that gives me the error 'invalid operands to binary expression Creature and long', dereferencing the pointer to a creature shouldn't give me a null ever (actually it is the pointer that should be null I believe so dereferencing will throw an error) Commented Nov 15, 2014 at 0:35
  • @MohammadAliBaydoun I am doing this for my own understanding and enjoyment, but thanks for the suggestion Commented Nov 15, 2014 at 0:35
  • @user3667450 - Isn't there enjoyment in creating a working program that has no bugs in a quick amount of time? Commented Nov 15, 2014 at 0:46
  • 1
    @user3667450 - If you want to see how to create the null terminated array using std::vector: std::vector<Creature*> creatures(numCreatures, new Runner(maze)); creatures.push_back(0); The rest of your code basically stays the same. Commented Nov 15, 2014 at 1:17

2 Answers 2

2

Your creature is a pointer to a Creature. If you increment this pointer, you will point to the next Creature behind the currently pointed one and not to the next pointer in your table.

Use:

for (int i=0; creatures[i]!=nullptr; i++) {
        creatures[i]->travel();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The access loop should be:

for (int i = 0; creatures[i] != NULL; i++) {
    Creature *creature = creatures[i];
    creature->travel();
}

Your loop is treating creatures[0] as an array of creatures, but it's just a single creature.

If you want to do the loop with pointer arithmetic, it should be:

for (Creature **creature = &creatures[0]; *c != NULL; c++) {
    (*creature)->travel();
}

5 Comments

Wouldn't it be for (Creature **creature = ... then ?
Would it then make more sense to have Creature *creatures = new Creature[num], rather than creatures as Creature **creatures?
That won't work if you're making use of virtual inheritance, so the array can contain pointers to different subclasses of Creature. That seems to be what you're doing in createCreatures, you're setting them to new Runner instead of new Creature.
That doesn't work, because then the compiler complains that creature (in the for loop) cannot be set equal to creatures (aka a Creature* cannot be set equal to a Creature**) which is what &creatures[0] is
I think I got it wrong in the second block, it should be Creature **creature.

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.