Pointer Objects
Why create objects through pointers on the heap? Is there an advantage to doing this rather than just creating an object regularly?
tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Imagine you''re opening a text document. You cannot plan in advance how large it is going to be. Thus you never allocate a static array large enough to handle all cases... and if you did, it would be incredibly wasteful when only opening small documents.
Dynamic memory allocation let you get around that by literally ''creating new variables'' at run-time, of a size of your choosing.
Additionally, statically allocated variables only live as long as their enclosing scope, not so with dynamically allocated variables, whose lifetime is controlled by the programmer.
And there is no such thing as "creating an object regularly"
[ 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 ]
Dynamic memory allocation let you get around that by literally ''creating new variables'' at run-time, of a size of your choosing.
Additionally, statically allocated variables only live as long as their enclosing scope, not so with dynamically allocated variables, whose lifetime is controlled by the programmer.
And there is no such thing as "creating an object regularly"
[ 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 ]
"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
I mean like:
class1 object1regular;
instead of:
class1 * object1heap = new class1;
Also what you''re explaining to me sounds like linked lists.
tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)
class1 object1regular;
instead of:
class1 * object1heap = new class1;
Also what you''re explaining to me sounds like linked lists.
tcache
Contact Me
-----------
Games don''t affect kids. If Pac-man had affected us, we''d all be sitting in darkened rooms munching pills and
listening to repetitive electronic music. -- Kristian Wilson(Nintendo Inc., 1989)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Not so much linked lists, as just enough space to handle what you are doing rather than too much or too little. Dynamically allocating memory means you can allocate what you want, when you want. Yeah you are responsible for freeing it up again, but it means you aren''t restricted by a predefined choice.
Another thing it does is to control the lifetime of the object, if you create on the stack it will be destroyed when going out of scope, if you create it on heap, it will live forever (well, until you delete it).
Update GameDev.net system time campaign:
Update GameDev.net system time campaign:
''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement