Problem with Dynamic Allocation of Class array

Hello. I have been at this for a week and it still confuses me.

Basically, I have a class, class_bot, coded in a .h file. It also has some inheritance of another class, class_physics. I am only bringing that up because I think it's relevant.

Anyway, my problem is this: I have a different class, class_map, which is meant to allocate a Dynamic Array of the class_bot class. It does this like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class class_map
{
private:
    SDL_Surface* BMP_bot;

    class_bot *bot;

......................


void class_map::load()
{
    bot = new class_bot[7];

    if (bot)
    {
        BMP_bot = IMG_Load("data/en_bot.png");

        for (int i = 0; i < 6; i++)
        {
            bot[i].create(i*40, 20); //This basically sets its x and y positions
            bot[i].load(BMP_bot);    //This passes a pointer to the sprite it draws.
        }
    }
}

.......................

void class_map::draw( SDL_Surface* BMP )
{

    for (int i = 0; i < 5; i++)
    {
       bot[i].step(room); //Draw
    }
}

..............................

void class_map::destroy( )
{
    delete []bot;
    if(BMP_bot!=NULL)       SDL_FreeSurface(BMP_bot);
}


So basically, all I'm trying to do is allocate an array of 7 bots, and draw only 5 of those (I was attempting to use trial and error earlier).

However, no matter what I do, everytime I run the game either a random amount of bots show up, they perform actions which indicate their 'gravity' value was not properly set, despite it being set in their constructor, or the game refuses to open alltogether and just crashes. I know this is because of the memory allocation, but I honestly don't know why this is happening. Somebody, please help. Thankyou.

EDIT: Also, when no dynamic memory allocation is used, the bots operate perfectly every time.
Last edited on
I don't see in your code the place where you set the gravity. Is this in the default constructor?If yes it should be fine because line 13 call default constructor.

For the dynamic allocation it looks fine. Maybe the class_map::draw function is called before class_map::load or after class_map::destroy. Anyway, use std::vector or std::list if you are allowed, it will save you a lot of trouble.
And another thing, you have i < 6 at line 19 but you allocate 7 bots at line 13, the 7th sprite is not initialized.
With the < 6, < 7 part, I am well aware of that mistake. It was the result of me getting a bit desperate and trying random values.

Now, I tried using lists, and, THANK YOU! At the very least, they worked. They worked 100%... for now. Also as it turned out, I had an integer in the bot object: "flash" which I had used to make the bot become invisible temporarily. I had forgotten to initialize the value however, and as such, the bots would randomly start the game already invisible, which was what was confusing me.

Something to note: Back when I was using Dynamic Arrays, gravity wasn't the ONLY thing that was effected. Sometimes even unrelated objects would behave oddly, and one time, the overlay text I had been using to check various values quadrupled in size. But once I started using vectors, that all went away. Thankyou very much, I hope it'll be a while now before my next error of this magnitude.

EDIT: Also, I know this is unrelated, but is there a way to pass different classes into the same parameter. Like, say I have class_bot and class_slime. Both of them contain a load() and step() function that needs to be run. So is there a way to get a function such as:

fn_initenemy( void anyememyclass )
{
anyenemyclass.load();
}

I know it's unrelated; It's just that I don't know what the proper name for this action is.
Last edited on
First method, the OOP one:
create an interface(a class with only pure virtual methods and no attributes), say ILoadable with the following syntax:
1
2
3
4
struct ILoadable
{
virtual void load() = 0;
}


Then inherit class_bot and class_slim with ILoadable:
 
class class_bot : public ILoadable


and don't forget to make the load method of class_bot and class_slime virtual

Then your function will look like:
1
2
3
4
fn_initenemy( ILoadable& anyememyclass )
{
anyenemyclass.load();
}

That way the right load method is called.

Second method, the template one:
1
2
3
4
5
template <typename T>
fn_initenemy( T& anyememyclass )
{
anyenemyclass.load();
}

This way you can call fn_initenemy with any class which have the load method.


The template method have the advantage to be faster to execute but slower to compile and make the executable file bigger.
I second the interface implementation, powerful, flexible and scalable.
Yes, this is also the one I advise but the template one can be useful when you don't want overhead.
Oops, sorry. I missed the email notification of this one. Thanks for all your help, and aquaz, the method you suggested worked perfectly, thankyou!
Topic archived. No new replies allowed.