0
static void spawnEnemies(void)
{
    Entity *enemy;

    if (--enemySpawnTimer <= 0)
    {
        enemy = malloc(sizeof(Entity));
        memset(enemy, 0, sizeof(Entity));
        stage.fighterTail->next = enemy;
        stage.fighterTail = enemy;

        enemy->x = SCREEN_WIDTH;
        enemy->y = rand() % SCREEN_HEIGHT;
        enemy->texture = enemyTexture;
        SDL_QueryTexture(enemy->texture, NULL, NULL, &enemy->w, &enemy->h);

        enemy->dx = -(2 + (rand() % 4));

        enemySpawnTimer = 30 + (rand() % 60);
    }
}

This is the code that i have and i tried using new insted of malloc but it doesnt seem to work i tried now vector but it didnt work. https://github.com/brandonto/sdl-space-shooter.git this is a link to the code i have at the moment

  • 2
    Replace `malloc` with `std::unique_ptr` or `std::vector`. – Eljay May 17 '20 at 13:58
  • 1
    Search "dynamic memory allocation c++". Anyways malloc works in c++ to. – Bayleef May 17 '20 at 13:59
  • @BaileyKocin Yes it works, but it might not do what's expected when creating objects. And it won't work without some casting. In short, better use C++ specific facilities. – Some programmer dude May 17 '20 at 14:01
  • See [this](https://softwareengineering.stackexchange.com/questions/274801/raw-weak-ptr-unique-ptr-shared-ptr-etc-how-to-choose-them-wisely) for more on `std::*_ptr` – ljleb May 17 '20 at 14:01
  • 2
    @AlphaTears I suggest you invest in [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn C++ properly first. – Some programmer dude May 17 '20 at 14:02
  • 2
    The main reason (and difference between C and C++) is that `malloc()` is used in C at best without a type cast as it returns `void*` which is compatible to any typed pointer. That isn't anymore true in C++. Thus, the type cast has to be added here: `enemy = (Entity*)malloc(sizeof(Entity));`. However, in C++, there are a lot other techniques to prevent `malloc()` at all, (e.g. `enemy = new Enemy();` which has to be paired with `delete enemy;` instead of `free(enemy);`) which were already mentioned above. – Scheff's Cat May 17 '20 at 14:07
  • O.T.: `stage.fighterTail->next = enemy;` Isn't it lacking a previous test `if (stage.fighterTail)`? If not I would instead add an `assert(stage.fighterTail);` to check and document that this is based on the assumption that the `stage.fighterTail` may never contain a null pointer (`nullptr`) at this point. – Scheff's Cat May 17 '20 at 14:14
  • i actually tried using the new function but it didnt work for me didnt know we would have to pair it with a delete() tyy – AlphaTears May 17 '20 at 14:14
  • `new` and `delete` are operators in C++. Hence, the parentheses are left out intentionally in my above comment. (There are some rare cases where additional parentheses may change the meaning. Hence, it's not only a matter of style to add parentheses here and there for somebodies own joy.) ;-) – Scheff's Cat May 17 '20 at 14:20
  • Please show the code you have tried and explain the problem you are having with it (a [mre]) – Alan Birtles May 17 '20 at 14:30
  • @AlanBirtles https://github.com/AlphaTears/SDLSpaceShooter.git the error starts within the Stage.cpp – AlphaTears May 17 '20 at 14:42
  • Please edit your question to contain a [mre] – Alan Birtles May 17 '20 at 14:52

0 Answers0