Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tai-Pan

objects in memory

This topic is 5634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey people, suppose I have the following classes: class NumInfo { public: short num; short x; short y; char colornum[5]; //constructor NumInfo(void) { char temp[5]="nad0"; strcpy(colornum, temp); num=999; x=999; y=999; } ~NumInfo(void) {} }; //********************************************************************************* class PlayerInfo { public: PLInfo Jugador; //PLInfo is another classe defined somewhere else NumInfo Numero; //constructor PlayerInfo(void) {} PlayerInfo(const PlayerInfo& playerinfocopy); ~PlayerInfo(void) {} }; PlayerInfo:layerInfo(const PlayerInfo& playerinfocopy) { Jugador=playerinfocopy.Jugador; Numero=playerinfocopy.Numero; } //**************************************** class TeamInfo //informacion de un equipo { public: PlayerInfo Team[6]; //este es un quipo formado por la informacion para cada jugador (6jugadores) TeamInfo(void) {} TeamInfo(const TeamInfo& teaminfcopy); ~TeamInfo(void); {} }; TeamInfo::TeamInfo(const TeamInfo& teaminfocopy) { for(short p=0;p<6;p++) Team[p]= teaminfocopy.Team[p]; } //----------------------------------------------------------------------- If I define somewhere in my main function the following object: TeamInfo* someteam = new TeamInfo(); I need to know if EVERY object inside someteam will be defined in the heap, for example, the following object: someteam->Team[2].Numero.num num is defined in the heap or in the stack? (remember that someteam is a pointer) Also..in this case..Has Numero´s constructor been called or not? Are constructors only called when you "new" an object?! Thanks in advance.. "Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"

Share this post


Link to post
Share on other sites
Advertisement
If you call new on an object, it will be allocated on the heap. Objects inside of it will be either allocated inline(on the heap), or also on the heap.

Constructors are called whenever you create an object:

  
NumInfo n; //here

NumInfo* n = new NumInfo; //here

struct B
{
NumInfo n;
};
B b; //here

char buffer[1000];
NumInfo* n = new(buffer)NumInfo; //here

vector<NumInfo> nv(10); //here

etc...

Share this post


Link to post
Share on other sites
"Constructors are called whenever you create an object"

I''m fairly new to c++ trying to make the move from procedural C.
With a class I was working on, if I created an instance of the class globally, it would not call the constructor, nor will it call the deconstructor when it went out of scope.
(I know because it wrote to a file to tell me)

But when I used new() and delete() on it, it did call them.

It was just a plain class with no fancy C++ features.. Basically C structs with a few private members and some functions in it.
Can anyone explain why?

Share this post


Link to post
Share on other sites
The constructor and destructor are indeed called for global object variables. However, I don''t think the order of construction and destruction is defined between global variables so your file stream object was probably destroyed before your object variable.

Share this post


Link to post
Share on other sites
sorry..I didnt understand this: "..Objects inside of it will be either allocated inline(on the heap), or also on the heap."
what do you mean with the word "inline"?


"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"

Share this post


Link to post
Share on other sites
could you help me with this part?:
I need to know if EVERY object inside someteam will be defined in the heap, for example, the following object:

someteam->Team[2].Numero.num

num is defined in the heap or in the stack? (remember that someteam is a pointer)

Its not clear for me...thank you

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"

Share this post


Link to post
Share on other sites
let''s make the example a little smaller to explain ...

here''s a class


  
class TeamRoster
{
public:
TeamRoster(void);
~TeamRoster(void);

Team& GetTeam(unsigned teamIndex);
Leauge& GetLeague(void);

protected:
Team teams[numTeams];
League *league;
};


When you construct an item of type TeamRoster, all of its member data is located wherever you create it (if you create it in a function, it''s on the stack, if you use new, it''s on the heap) What this means is that the actual team array is wherever ''it'' is, and so is the actual league pointer ... but the league itself is somewhere else, and simply pointed to. Where is the leauge, I don''t know, it depends on how you created it ... if you used new, it is on the heap, if not, it is either on the stack or in the data segment ... all of these rules are EXACTLY the same as using C ... everything allocated with malloc / free (new / delete) is on the heap, everything else is on the stack or in the program''s data segment.

What about internal members of the teams? The actual member data of the teams is inside of the TeamRoster object ... because it is an array, NOT a pointer ... so they are on the heap if the roster is, etc ... BUT if they have pointers internally, then those pointers can point ANYWHERE, depending on your class code ... if they call new to construct "Player" objects internally, then you might have a situation where the team roster and the teams are on the stack ... but the players are on the heap ...

Share this post


Link to post
Share on other sites
an a contructor is called for all objects when they are created (if the class has one), no matter what ... so your test must be flawed ... a destuctor SHOULD be called for all objects ... but there are two error cases where this doesn''t happen ... if you allocate an object with new, then the destructor only gets called if you use delete to free the object ... otherwise you have a memory leak, and if you throw an exception in an object''s constructor, then that objects destructor is not called ... (this is correct behavior, by design ... the destructor is called for all COMPLETELY CONSTRUCTED objects, not objects which fail during construction - because it is assumed that when you detect an error during construction, you will clean up internally, THEN throw the exception).

Share this post


Link to post
Share on other sites
I have a question, when you make an array of objects why can't you call at the same time a specific constructor, ex:>

class MyClass
{
public:
MyClass();
MyClass(int x);
};

MyClass* MyObject = new MyClass[100](10);

Why can't this be done?

[edited by - pag on April 22, 2003 7:18:09 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by pag
I have a question, when you make an array of objects why can''t you call at the same time a specific constructor, ex:>

class MyClass
{
public:
MyClass();
MyClass(int x);
};

MyClass* MyObject = new MyClass[100](10);

Why can''t this be done?



Sadly, this is just not possibly in the C++ language. You''re better off making an explicit "init" function and calling that in a loop afterwards.

Regards,
Jeff

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!