Jump to content
  • Advertisement

Archived

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

smokes

Dynamic memory allocation and object design

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

I've read somewhere on this site that: What new's something should also delete it.. is this true? could any of you direct me to a tutorial dealing with object design? Lets say I got this Mouse class which have an update function.. for it to update its cursor it will need to blit it to the screen.. I just wrote this mouse class and found out I did this: I have a CSurface class wrapping directdraw surfaces.. my mouse class uses this to blit the mousepointer.. in the mouse class I had a pointer CSurface *MousePointer.. I set it using the constructer and in the destructor I delete it (but it is new'ed in the main program).. and in my main program I only created it and handed to pointer to the class.. this is bad design right? I should either pass the pointer and delete the mousepouinter surface from the main program or let the mouse class create the mousepointer surface and delete it.. Am I right? or I have just misunderstood the concept? :-).. I am rewriting my 2d engine.. I want it done right this time.. but haven't found any resources on the net teaching good object/code design..any of you know some? im sorry for the rather confusing title.. makes this post sound a lot more exciting than it is :-) * just edited this post.. Was obviosly pretty tired last night [edited by - smokes on May 30, 2002 6:19:31 AM]

Share this post


Link to post
Share on other sites
Advertisement
Actually, your question is much more interesting than the title suggest

Memory de/allocation is a mudane subject. If you new it, delete it. If you malloc it, free it.
Whatever new/malloc's it, should delete/free it. Allocating memory from on object, and freeing it in another is poor design - it's weak encapsulation, prone to bugs & thus leaking, and full of caveats when using dynamic linking. Reference counting schemes seem to be in-vogue - you use a factory method to create the object, keep a reference count on it, and when it drops to 0, the object delete's itself, causing the release of the objects resources by invoking the destructor not the deconstructor (common newbie mistake ).

quote:

...but haven't found any resources on the net teaching good object/code design..any of you know some?


The artifact you seek is called "Design Patterns: Elements of Reusable Software" by Erich Gamma & et. al. It, however, is not free, nor exciting reading. In fact, it's the second most boring book I have. Though it was arguably the most enlightening.
On this site, there are two decent articles in the new monthly engineering column about singeltons (a singleton is a design pattern). That said, it is my opinion that much of the information about software design patterns on the web is misguided - don't rely on one source.

[edited by - Magmai Kai Holmlor on May 29, 2002 10:55:27 PM]

Share this post


Link to post
Share on other sites
thanks for the reply.. well I meant destructor.. wrote the post late last night :-)..

factories and reference counting.. that is stuff I want to learn.. but I haven''t found lot on the net.. I have written many lines of code during the last 8 years.. but never had any teaching or decent book on design.. When you are just a hobbyist you tend to just write code, ugly and buggy which get the job done.. While this approach may work for small projects, it really becomes a pain in the a** when working on larger projects :-)

funny I only got one reply.. I means this board is mostly populated by amateur coders.. there ought to be others strugling with the same problems..

somebody ought to write a tutorial on creating a memory manager for games.. But a simple one so everyone could follow it.. Gonna look into this singleton stuff now.. I don''t even know what it is *lol*

Share this post


Link to post
Share on other sites
Regarding the example of the mouse cursor: You say that you have a CSurface class that wraps DD surfaces. You also say that your mouse class uses such a CSurface to blit the cursor to the screen. It seems logical to me to inherit the mouse class from the CSurface class, since it is a more specialized use of a surface. You may need to inherit from some other base class too, depending on how your design is.

For this specific example, you certainly want only one instance of this mouse class to exist in your application. As you probably already found out, this is a "singleton". Implementing the mouse class as a singleton is not mandatory, but elegant.

Forever trusting who we are
And nothing else matters
- Metallica

Share this post


Link to post
Share on other sites
thanks but I like composition better in this case.. because I could make my mouseclass contain a linked list of CSurfaces thereby allowing me to change the mousepointer on the fly.. I am reading up on this singleton pattern.. yes it indeed looks elegant but I have never tried overwriting the new and delete operators yet.. Guess I should read up on advanced c++ as well :-)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!