Need help on understanding Dynamic memory more

Hey everyone, I have been dealing with how to actually use this for a while now and it has been stopping my progress on a program I'm trying to make. I'm working on a game and I'm trying to figure out what I would need to allocate. I was thinking the object I create because in a class there are alot of data members declared there such as int, float, string ect.

So lets say I have a class called Hero. In the class I put what this hero will have:

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Hero
{
protected:
int x;
int y;

int bx;
int by;

int HP;
int Energy;

ALLEGRO_BITMAP *hero1image;

//basically everything the hero will have including animation ect


}


and now in main of course after doing whatever is needed:

1
2
3
4
5
HeroImage = al_load_bitmap("Hero1.png"); // HeroImage was created using ALLEGRO _BITMAP*

Hero *hero = new Hero();

hero->InitObject(HeroImage); // A function to initialize the data members in  the Hero class for hero and also passing an image so that I can store it to the image member part of the Hero class 


Here would be what the InitObject(); does:
1
2
3
4
5
6
7
8
9
Hero::InitObject(ALLEGRO_BITMAP *Image)
{
x = 300;
y = 200;

//ect..

hero1image = Image;
}


so after I do all of this, do I have to delete the object in the destructor or do I need to make a function to delete the object later or do I delete the object hero in main after done using it? this is what confuses me is when do I delete it and if I passed the object to another function do I have to delete in that function or pass it back out of the function back to main?

Thanks if anyone can help me here cause I'm really confused and I want to pass object I allocate of the heap so I can manage to not use alot of memory. So basically I just want to know how do I pass objects around and when to delete the object or pretty much anything I allocate like variables storing images since they take memory.

also if anyone can suggest any other libraries or other tools to use to improve my programming that would be great, btw is Qt also a good program to use to get more better at programming? thanks again.
closed account (o3hC5Di1)
Hi there,

You will want to look into class constructors and destructors.
Constructors are called automatically when an object is created and can be redefined to accept various arguments too.
Destructors are called automatically when an object is destroyed, allowing you to delete all the dynamically allocated data it contains.

In your case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Hero
{
protected:
int x;
int y;

int bx;
int by;

int HP;
int Energy;

ALLEGRO_BITMAP *hero1image;

public:
   Hero() : x(300), y(300) {} //default constructor sets x and y to 300
   Hero(ALLEGRO_BITMAP *img) x(300), y(300), hero1image(img) {} //constructor taking pointer to an allegro_bitmap
   ~Hero()  //destructor
   {
      //destroy dynamically allocated memory here - you don't have any in this class it seems
     //but as an example let's destroy an imaginary array of items
      delete[] heroitems;
   }
}


Note how the constructor replaces your "initialise" function.

For more information, check:
http://cplusplus.com/doc/tutorial/dynamic/
http://cplusplus.com/doc/tutorial/classes/ (construcotrs / destructors bit)

Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.