Re: Need design advice

From: Ivan Vecerina (please_use_web_form_at_ivan.vecerina.com)
Date: 11/06/03


Date: Thu, 6 Nov 2003 11:51:36 +0100


"Patrick Kowalzick" <Patrick.Kowalzick@cern.ch> wrote in message
news:bod0hv$5s9$1@sunnews.cern.ch...
| Do you mean here, that I avoid all the pifalls when I throw exceptions
| inside classes with pointers (allocating memory) when I just use a smart
| pointer? This in fact would be very nice :-).
Well, it can help with a lot of the issues. But smart pointers
themselves aren't always trivial to use either...

| Coming back to the topic.
|
| Assume my object exists e.g. a Point "p", and I have to pass it to another
| class "bar". I could do this via a proxy "bar_proxy". "bar" needs "p" for
| quite a long time, and I could not be sure, that "p" still exists, when
| "bar" needs it. What means, I need here a kind of "delayed copying", that
in
| case the Point "p" is destructed, the "bar_proxy" creates an instance.
[...]
There are a few possible approaches.
 - first, avoid copies as much as possible by passing objects
   by reference when possible.
 - the objects themselves may choose to implement some form of
   reference counting, with or without automated COW (copy-on-write).
There are many options really.
I would suggest studying Scott Meyer's "(More) Effective C++" books
regarding these topics. You may also read about std::string (which
currently is typically implemented without reference counting,
because it is complex and inefficient in multithreaded apps).
Also, consider using boost::shared_ptr (from www.boost.org)
as a smart pointer for the contained data.

What you really need to chose AFAICT is whether a type will
automatically share its data among object copies or not.
 If not, the user is responsible to take care of lifetime issues.
 If yes, you need to decide wheter this sharing will be
  visible to the users or not (if not, COW needs to be implemented).

| For a huge amount of Data (1000dim Point ;-) ) I assume that the class
Point
| itself is smart enough to do this job, what means inside my "bar_proxy" I
| just create another "Point pp = p".
| For small data-types (double, int,....) I just create a copy as well.
|
| But what shall I do for "medium-sized" data-types ?

There is no general rule about which approach is better.
Testing and tuning is typically required -- as for all optimizations
attempts (avoiding data copies is an optimization...).

Regards,
Ivan

-- 
http://ivan.vecerina.com


Relevant Pages

  • Re: Strong/weak pointers
    ... since the pointers can indirectly reference ... the programmer to avoid. ... so you should not allow comparison of pointers except for equality ... That's why I wonder if reference counting isn't worth considering, ...
    (comp.compilers)
  • Re: How to get Interface reference counting in ATL
    ... Smart pointers are there to assist the programmer. ... the underlying interface pointer any more, ... personal preference but not necessarily yours. ...
    (microsoft.public.vc.atl)
  • Re: pMedialControl->Stop() hangs indefinitely (sometimes)
    ... >>> the pointers they're holding, because COM doesn't like pointers being ... > So before CoUninitialize, do I release or not all my smart Com pointers? ... If your classes are well-designed your destructor will call the destructors ... on the smart pointers when the graph is deleted, ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: IDispatch question ...
    ... In this case we would be talking about smart pointers instead of (plain) old interface pointers. ... I strongly advise you to use smart pointers instead of raw COM pointers, as they take care of a lot of things. ... If you like inside the CreateInstance method of the template _com_ptr_t, you'll see that this method is performing exactly the steps that are necessary to instantiate an object with the given class ID and retrieve the requested interface pointer. ...
    (microsoft.public.vc.language)
  • Re: simple delete question
    ... >>pointers in your code, so why did you consider using new? ... > was not aware that dynamic allocation had a high cost at all. ... point of smart pointers is that they are much less error prone. ...
    (comp.lang.cpp)