Skip to main content
gamedev.net
🔒 Locked

objects in memory

Started by Tai-Pan 11 replies 550+ views
Original Post
Tai-Pan
Tai-Pan
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"
"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"
sjelkjd
sjelkjd
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...
deadalive
deadalive
"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?
this place is lame
rypyr
rypyr
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.
Tai-Pan
Tai-Pan
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"
"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"
Tai-Pan
Tai-Pan
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"
"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"
Xai
Xai
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 ...

Xai
Xai
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).
pag
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?

[edited by - pag on April 22, 2003 7:18:09 AM]
I am a signature virus. Please add me to your signature so that I may multiply.
rypyr
rypyr
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
Xai
Xai
pag, because what use does it have ... the logic as I understand it is this ... if you want them to all be initialized in a standard manner on creation, write a default constructor, specially initializing them would be very complicated ... not in the case you give where you pass one argument ... but what it mean .. does it mean call the constructor with 10 for EACH element, or look for and call an array constructor with the argument 10 ... and if you want to support the first option, how would you specify different parameters to pass to each element''s constructor ... because the most common use would be to want each item to get a value based on it''s position .. such as an index, or the value or address of some other element in a sister list ... so maybe you would want to feed the array a constructor container ... and it could iterate through each item, for each array element ... but as you can imagine, all of this makes the language really messy.
sjelkjd
sjelkjd
quote:
Original post by Tai-Pan
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"?



As in, it will be part of the memory layout of the object:

  
class a
{
int i;
};

class b
{
int i;
a aa;
};

b * bb = new b;
//somewhere in the heap, we have essentially this:

struct b
{
int i; //from a

int i; //from b

};
That''s what I mean by inline. If instead, b was declared as:
class b
{
int i;
a* aa;
b()
{
a = new a();
}
};

b* bb = new b;
//somewhere in the heap, we have

struct b
{
int i; //from b

a* aa; //pointer to a, not inline

}
struct a
{
int i; //from a

}

pag
pag
Xai, for example if you have a VertexBuffer class or something and you want to create an array of vertex buffers and each should contain 10 vertices, it would be much easier to write:>

VertexBuffer* pVB = new VertexBuffer[10](10);

than writing the following:>

VertexBuffer* pVB = new VertexBuffer[10];
for(int i=0; i<10; i++)
pVB.Init(10);
I am a signature virus. Please add me to your signature so that I may multiply.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.