Skip to main content
GameDev.net gamedev.net
🔒 Locked

Static vs. Dynamic allocation for a class

Started by Riskbreaker Jan 29, 2003 at 1:31 PM 4 replies 400+ views
Original Post
Riskbreaker
Riskbreaker
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!
--RiskbreakerCoding Soul
Qw3r7yU10p!
Qw3r7yU10p!
is this homework?

just thought i''d better ask
Riskbreaker
Riskbreaker
*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]
--RiskbreakerCoding Soul
Fruny
Fruny
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
MattB
MattB
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.

Topic Locked

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

Sign in to reply to this topic.