Space allocation and class pointers.

Started by
4 comments, last by Tai-Pan 21 years ago
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"
"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"
Advertisement
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]
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;...)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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"
"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 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.
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"
"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"

This topic is closed to new replies.

Advertisement