Archived

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

Riskbreaker

Static vs. Dynamic allocation for a class

Recommended Posts

Riskbreaker    122
Can anyone list the pros/cons between these two methods? Class newclass; Class *newclass = new Class(); I''ve been wondering about this since I started C/C++ programming. I would have thought the second would be easier to pass around, but you can pass the address of the first one just as easily. Thanks for your thoughts!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I can give you some things to think of.

What is the difference in lifetime? When are constructors/destructos called in both cases?

Where is the memory allocated? From the heap or stack? You need to learn the difference and understand why heap allocations are slower than stack allocations. But that doesn''t mean putting objects on the stack always is good (hint: large objects, lifetime issues).

If you have a long function (which you should avoid...) and new an instance in the beginning and delete it in the end. What do you think will happen if you put "return" in the middle somewhere? Compare what would happen if the object was allocated from the stack instead.

Share this post


Link to post
Share on other sites
Riskbreaker    122
*Sigh* so much for straight-forward answers...
No, this is not homework. This is something I've honestly been curious about since I started with C. AP, you give some good points on the different kinds of memory location. I believe the constructors and destructors are called during runtime for dynamic setups and before/after runtime for static setups. I understand the variation in heap and stack location as well. Are these memory variations the only difference?
I'm beginning to think I should use stack allocations for more 'permanent' data, and heap allocations for more 'temporary' data.

New Thought: What if you dynamically allocate a class that has a member declared as so:

int a[1000];

Where is that memory allocated to? Is the class given its own stack area when it's allocated?

[edited by - Riskbreaker on January 30, 2003 1:47:28 AM]

Share this post


Link to post
Share on other sites
Fruny    1658
static variables exist for the whole program's life.
They live in the statics section of the memory.
Their existence is determined at compile-time.
Their number is fixed.

automatic variables exist until the function they're declared in returns.
They live on the stack.
Their existence is determined by the program's structure.
Their number is fixed.

dynamic variables exist until you destroy them manually.
They live in the free store (heap or other).
Their existence is determined at run-time.
Their number is unbounded.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on January 30, 2003 2:07:11 AM]

Share this post


Link to post
Share on other sites
MattB    122
The stack is typically a lot smaller than the heap, and certainly faster to allocate from. Use the stack for short-lived ''temporary'' data. Use the heap for larger allocations.

quote:

New Thought: What if you dynamically allocate a class that has a member declared as so:

int a[1000];

Where is that memory allocated to? Is the class given its own stack area when it''s allocated?



An object''s data members exist wherever you allocated the instance of your class. The only exception is static variables, which always exist on the heap.

Share this post


Link to post
Share on other sites