Archived

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

Space allocation and class pointers.

This topic is 5376 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

Suppose I have this: class X { int x; char etc; X(){} }; class Yeah { string nope; X classvariable; Yeah(){} }; Suppose that somewhere I declare this: Yeah* object = new Yeah(); Ive been wondering if ALL the variables (attributes like char etc, int x, string nope) contained in the two classes will be stored in the stack (since I created the "object" variable with new) OR if they will be created where all automatic variables are created?! Any help? "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
anytime you use new, objects get allocated in the heap (dynamic memory store which is usually much bigger than the stack space you have available). variables declared like the following variable "poo" are allocated on the stack:


void foo {
int poo;
}


[EDIT: oh right. so to answer your question. the member variables of yeah will be allocated on the heap b/c that's part of allocating the class]

-me

[edited by - Palidine on March 24, 2003 7:03:36 PM]

[edited by - Palidine on March 24, 2003 7:04:11 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Tai-Pan
Ive been wondering if ALL the variables contained in the two classes will be stored in the stack OR if they will be created where all automatic variables are created?!



Automatic variables are created on the stack, unless they are optimized into a register only existence, e.g. for(int i=0;...)

Share this post


Link to post
Share on other sites
Im confused..Palidine tells me this: "[EDIT: oh right. so to answer your question. the member variables of yeah will be allocated on the heap b/c that''s part of allocating the class]"; but then Magmai Kai writes this: "Automatic variables are created on the stack, unless they are optimized into a register only existence, e.g. for(int i=0;...)"

Lets clear this up: All the automatic variables contained inside the class declared with new will be stored in the heap and NOT in the stack?!



"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
Tai-pan I think you''re using a different definition of automatic variable.

If you allocate something with new, it will be created on the heap. If whatever you allocate is a class/struct, then that includes all member variables (like the string nope and X classvariable in your example). Any variable just declared without new, such as "int i" or "SomeClass someInstance" will be allocated on the stack, again if it''s a struct/class that includes all the member variables.

Share this post


Link to post
Share on other sites
Yeah..I got confused about the automatic definition, now im SURE what an automatic variable is..sorry.
And now thanks to you my doubt is gone as well..thanks Dobbs.

"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