0

So, I'm trying to send a pointer vector to a function, where I add some new members to it with the .pushBack() function. This works when I want only one member on the vector. But if I try to push more it throws a :

**terminate called after throwing an instance of 'std::bad_alloc'

what(): std::bad_alloc**

I've tried several things, this is my last attempt where I use a void ptr and a static cast to the vector, because I read that using the & creates a temporary address and so I've tried a more permanent one.

So, here it goes:

void    humans(std::vector<Character> *human, unsigned human_count) {

    for (unsigned i = 0; i < human_count; i++) {
        unsigned human_x;
        unsigned human_y;
        unsigned human_id;
        std::cin >> human_id >> human_x >> human_y; std::cin.ignore();

        human->push_back(Human(human_id, Coordinates(human_y, human_x)));
     }
}

int main()
{
    unsigned                x;
    unsigned                y;
    Ash                     Pete;
    std::vector<Character>  human;
    void                    *ptr;
    Character               *zombie;
    unsigned                human_count;
    unsigned                zombie_count;

    ptr = &human;
    while (1) {
        std::cin >> x >> y; std::cin.ignore();
        Pete.setPosition(Coordinates(y, x));
        std::cin >> human_count; std::cin.ignore();
        std::cerr << "HUMANS " << human_count << std::endl;
        humans((static_cast<std::vector<Character>*>(ptr)), human_count);

        std::cin >> zombie_count; std::cin.ignore();
        zombies(&zombie, zombie_count);
        human[0].print();
        human[1].print();
        if (human[1].getId() == 1)
            std::cout << human[1].getPosition() << std::endl;
        else
            std::cout << human[0].getPosition() << std::endl;
    }
}

It throws the error on the humans function. Thank you!

  • One should almost *never* have pointers to containers. What have your text-books, tutorials or teachers said about *references* and how to pass arguments by reference? – Some programmer dude Sep 25 '22 at 16:14
  • And all those `std::cin.ignore()` calls looks like some sort of [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). They are not needed. – Some programmer dude Sep 25 '22 at 16:16
  • Also please use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch the exception as it's thrown, so you know exactly when and where in your code it comes from. – Some programmer dude Sep 25 '22 at 16:18
  • `std::vector` has method `size()`, why do you need to pass another variable, which most probably has invalid value? – Slava Sep 25 '22 at 16:19
  • Is `Human` a class inherited from `Character`? If so, what you are trying to do here can't work. Use `std::vector>` instead. Otherwise you are just slicing your `Human` object to a `Character` when storing it in the vector. Also you _must_ give `Character` a virtual destructor for this to work. In any case, context is missing to say anything definitive. Please provide a complete [mre]. – user17732522 Sep 25 '22 at 16:19
  • @Slava That's the target size of the vector, not the original one. The function is written unconventionally. It should be returning the vector not taking it as an out-parameter by-pointer. – user17732522 Sep 25 '22 at 16:21

0 Answers0