Questionable code example in C++ FAQ Lite?

Discussion in 'C++' started by Juha Nieminen, Dec 28, 2008.

  1. Consider the code example here:

    http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9

    It needlessly allocates an object dynamically and, what's worse, it
    never deletes it.

    Let me emphasize that I understand perfectly that this is done to keep
    the example short and simple, and that I understand that *how* the
    object is created is completely irrelevant with respect to what the
    example is trying to show. (In other words, the allocation of the object
    is not the point of the example, but how the object is used in the
    subsequent lines.)

    However, given that, AFAIK, this FAQ tries to teach good programming
    practices in C++, this is a horrible example of that. Even though this
    has been done to keep the example simple, there's always the danger of a
    beginner programmer seeing this and getting the impression that it's
    normal and ok to allocate temporary objects like this and leave them
    hanging. Even if allocating the object dynamically would be excusable
    (for the sake of simplicity), not deleting it certainly isn't.

    (Btw, I once contacted the author of the FAQ about this issue by
    email. I clearly explained that I understood why it's done like that,
    ie. to keep the example simple, but that I still think it's a bad idea
    to teach bad habits to beginner programmers like this. His reply was
    extremely disrespectful, arrogant and outright insulting. The example
    code was naturally never fixed. I really couldn't understand that kind
    of response.)
     
    Juha Nieminen, Dec 28, 2008
    #1
    1. Advertising

  2. Juha Nieminen

    alfps Guest

    On 28 Des, 10:26, Juha Nieminen <> wrote:
    >   Consider the code example here:
    >
    > http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9
    >
    >   It needlessly allocates an object dynamically and, what's worse, it
    > never deletes it.


    Yes.


    >   Let me emphasize that I understand perfectly that this is done to keep
    > the example short and simple, and that I understand that *how* the
    > object is created is completely irrelevant with respect to what the
    > example is trying to show. (In other words, the allocation of the object
    > is not the point of the example, but how the object is used in the
    > subsequent lines.)
    >
    >   However, given that, AFAIK, this FAQ tries to teach good programming
    > practices in C++, this is a horrible example of that. Even though this
    > has been done to keep the example simple, there's always the danger of a
    > beginner programmer seeing this and getting the impression that it's
    > normal and ok to allocate temporary objects like this and leave them
    > hanging. Even if allocating the object dynamically would be excusable
    > (for the sake of simplicity), not deleting it certainly isn't.


    You do have a point.

    Instead of pointers, this example should simply use references.


    >   (Btw, I once contacted the author of the FAQ about this issue by
    > email. I clearly explained that I understood why it's done like that,
    > ie. to keep the example simple, but that I still think it's a bad idea
    > to teach bad habits to beginner programmers like this. His reply was
    > extremely disrespectful, arrogant and outright insulting. The example
    > code was naturally never fixed. I really couldn't understand that kind
    > of response.)


    Marshall Cline is one of the most courteous and well-mannered persons
    on the face of the planet, so I don't understand this, but I suspect
    some Communication Failure(TM).

    Unfortunately I'm on Christmas holiday and so I don't have a direct e-
    mail address for Marshall here, and as I recall he has a bouncer on
    the one given on the FAQ site, where you get an automatic reply and
    have to confirm that you're not a spammer. But anyway I'm CC-ing this
    to that address; if it manages to sneak through the anti-spam measures
    I'm sure Marshall will fix things if he's able to.

    Cheers & hth.,

    - Alf
     
    alfps, Dec 28, 2008
    #2
    1. Advertising

  3. Juha Nieminen

    James Kanze Guest

    On Dec 28, 10:26 am, Juha Nieminen <> wrote:
    > Consider the code example here:


    > http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9


    > It needlessly allocates an object dynamically and, what's
    > worse, it never deletes it.


    So what? It's only an example, displaying a particular problem.

    > Let me emphasize that I understand perfectly that this is done
    > to keep the example short and simple, and that I understand
    > that *how* the object is created is completely irrelevant with
    > respect to what the example is trying to show. (In other
    > words, the allocation of the object is not the point of the
    > example, but how the object is used in the subsequent lines.)


    So what's the point?

    > However, given that, AFAIK, this FAQ tries to teach good
    > programming practices in C++, this is a horrible example of
    > that.


    In what way?

    > Even though this has been done to keep the example simple,
    > there's always the danger of a beginner programmer seeing this
    > and getting the impression that it's normal and ok to allocate
    > temporary objects like this and leave them hanging. Even if
    > allocating the object dynamically would be excusable (for the
    > sake of simplicity), not deleting it certainly isn't.


    Even beginning programmers write code more complex than the
    example. And are capable of understanding that it is only an
    example. Throwing the delete in there wouldn't hurt, but it
    would raise a number of other issues, irrelevant to the question
    being discussed. (Base doesn't have a virtual destructor, for
    example.) You could also define the object as a local
    variable---that's probably what I would have done---but you have
    to recognize the fact that when inheritance is involved, local
    variables are the exception, not the rule. All in all, the
    example is a good compromise.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 28, 2008
    #3
  4. James Kanze wrote:
    > On Dec 28, 10:26 am, Juha Nieminen <> wrote:
    >> Consider the code example here:

    >
    >> http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9

    >
    >> It needlessly allocates an object dynamically and, what's
    >> worse, it never deletes it.

    >
    > So what? It's only an example, displaying a particular problem.


    Can't you read the entire post before answering? I explained the
    reason why I consider it a dubious example.

    >> Let me emphasize that I understand perfectly that this is done
    >> to keep the example short and simple, and that I understand
    >> that *how* the object is created is completely irrelevant with
    >> respect to what the example is trying to show. (In other
    >> words, the allocation of the object is not the point of the
    >> example, but how the object is used in the subsequent lines.)

    >
    > So what's the point?


    Do you only read one paragraph at a time when you answer to someone's
    post?

    >> However, given that, AFAIK, this FAQ tries to teach good
    >> programming practices in C++, this is a horrible example of
    >> that.

    >
    > In what way?


    It seems to be so (given that I explain it immediately after that
    paragraph).

    > Even beginning programmers write code more complex than the
    > example. And are capable of understanding that it is only an
    > example.


    What does it even mean that "it is only an example"? Exactly which
    part of it is "only an example" which should not be taken literally? All
    of it? Part of it? Which part?

    Exactly how is the beginner programmer supposed to know that "btw,
    this part is only for the sake of an example; you should not do this in
    actual code"? Wouldn't it be better to simply avoid giving as example
    code which should not be written?
     
    Juha Nieminen, Dec 28, 2008
    #4
  5. Juha Nieminen

    Guest

    On Dec 28, 10:07 am, Juha Nieminen <> wrote:
    > James Kanze wrote:
    > > On Dec 28, 10:26 am, Juha Nieminen <> wrote:
    > >>   Consider the code example here:

    >
    > >>http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9

    >
    > >> It needlessly allocates an object dynamically and, what's
    > >> worse, it never deletes it.

    >
    > > So what?  It's only an example, displaying a particular problem.

    >
    >   Can't you read the entire post before answering? I explained the
    > reason why I consider it a dubious example.
    >
    > >> Let me emphasize that I understand perfectly that this is done
    > >> to keep the example short and simple, and that I understand
    > >> that *how* the object is created is completely irrelevant with
    > >> respect to what the example is trying to show. (In other
    > >> words, the allocation of the object is not the point of the
    > >> example, but how the object is used in the subsequent lines.)

    >
    > > So what's the point?

    >
    >   Do you only read one paragraph at a time when you answer to someone's
    > post?
    >
    > >> However, given that, AFAIK, this FAQ tries to teach good
    > >> programming practices in C++, this is a horrible example of
    > >> that.

    >
    > > In what way?

    >
    >   It seems to be so (given that I explain it immediately after that
    > paragraph).
    >
    > > Even beginning programmers write code more complex than the
    > > example.  And are capable of understanding that it is only an
    > > example.

    >
    >   What does it even mean that "it is only an example"? Exactly which
    > part of it is "only an example" which should not be taken literally? All
    > of it? Part of it? Which part?
    >
    >   Exactly how is the beginner programmer supposed to know that "btw,
    > this part is only for the sake of an example; you should not do this in
    > actual code"? Wouldn't it be better to simply avoid giving as example
    > code which should not be written?


    While I kind of agree the example has some faults, it's a good example
    of what it's trying to show, it's probably the case that a beginning
    programmer will learn the correct way to create and destroy objects
    sooner or later -- likely sooner. Also, a programmer who is reading
    about hidden base members may likely have seen new/delete before and
    is probably far enough along to not be corrupted by the example.

    In any case a single example that's missing a delete is not going to
    send a beginning programmer down a long path of suffering, failure,
    and humiliation on their way to learning C++ -- there are other things
    that will send them down that path far more effectively (such as
    posting questions to this newsgroup :).

    Also, the memory is freed when the process ends. Unless the destructor
    has side effects outside the process (e.g. committing data to disk or
    affecting external hardware), it's technically not really an issue,
    even if it is a bit dubious looking.

    Jason
     
    , Dec 29, 2008
    #5
  6. Juha Nieminen

    alfps Guest

    On 28 Des, 11:26, alfps <> wrote:
    > I'm sure Marshall will fix things if he's able to.


    Marshall writes that he'll add a 'delete' and "please pass on my very
    best to Juha Nieminen".

    Cheers & hth.,

    - Alf
     
    alfps, Dec 29, 2008
    #6
  7. Juha Nieminen

    Guest

    , Jan 3, 2009
    #7
  8. Juha Nieminen

    Labyrinth Guest

    In article <>, "" <> wrote:
    >On Dec 29 2008, 1:07=A0am, alfps <> wrote:
    >> On 28 Des, 11:26, alfps <> wrote:
    >>
    >> > I'm sure Marshall will fix things if he's able to.

    >>
    >> Marshall writes that he'll add a 'delete' and "please pass on my very
    >> best to Juha Nieminen".

    >
    >It's in the latest update:
    >
    >http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9
    >
    >Jason


    Well, as far as "C++ is protecting you from yourself" goes,
    I think Java does it much better.


    --
    Java MFC Goldmine collections contain over 50,000 articles
    on Java, C, C++, C#, VC and MFC in over 50 categories.

    Tens of thousands of code snippets and examples,
    expert opinions and views and tons of relevant links
    on each category.

    Sites contain only relevant articles on selected topics.
    All noise articles were filtered out.

    You can find an answer on any issue within minutes,
    if not seconds.

    JavaGoldmine:
    http://javagoldmine.by.ru/index.html

    Mirror:
    http://tarkus01.by.ru/index.html

    MFCGoldmine:
    http://mfcgoldmine.by.ru/index.html

    Note:

    Sites are indexed by Google. To find you exactly the articles
    you are looking for, use advanced search and specify site,
    e.g. for MFC Goldmine,

    Search within a site or domain: mfcgoldmine.by.ru

    For Java Goldmine Google searches, use tarkus01.by.ru,
    not javagoldmine.by.ru. It has the biggest Google index.

    If have problems accessing some article, use the mirror site.
     
    Labyrinth, Jan 3, 2009
    #8
  9. Juha Nieminen

    Grizlyk Guest

    > On Dec 28 2008, 4:47 pm, James Kanze wrote:
    > > On Dec 28, 10:26 am, Juha Nieminen <> wrote:
    > >
    > > It needlessly allocates an object dynamically and, what's
    > > worse, it never deletes it.

    >
    > So what?  It's only an example, displaying a particular problem.
    >
    > > Even though this has been done to keep the example simple,
    > > there's always the danger of a beginner programmer seeing this
    > > and getting the impression that it's normal and ok to allocate
    > > temporary objects like this and leave them hanging. Even if
    > > allocating the object dynamically would be excusable (for the
    > > sake of simplicity), not deleting it certainly isn't.


    By the way, there is interesting question - does examples of C++ must
    be most simple or most complete?

    Take in account, that the code

    {
    Derived* d = new Derived();

    ....
    delete d;
    }

    must not be used by programmers (there are some exceptions), you
    should write like this:

    {
    my_own_great_holder<Derived> d=new Derived;
    ....
    }

    From other side, there are some exceptions, where there are reasons to
    place objects in dynamic memory once time per execution and you never
    need to call destructor for the objects, like this:

    int main()
    {
    Derived* d = new Derived;
    Base* b = d;
    b->f(65.3);
    d->f(65.3);
    }

    And what is better?
     
    Grizlyk, Jan 5, 2009
    #9
  10. Grizlyk wrote:
    > By the way, there is interesting question - does examples of C++ must
    > be most simple or most complete?


    IMO example code which is designed to teach people C++ should not be
    broken.

    > Take in account, that the code
    >
    > {
    > Derived* d = new Derived();
    >
    > ...
    > delete d;
    > }
    >
    > must not be used by programmers (there are some exceptions)


    Well, if that's a bad example, then why cannot a *good* example be
    used instead? A short piece of good-quality, actual, realistic C++ code,
    rather than "don't do this, it's just an example". Especially since the
    example doesn't say so, it just assumes the reader knows.
     
    Juha Nieminen, Jan 6, 2009
    #10
  11. Juha Nieminen

    Grizlyk Guest

    > On Jan 6, 3:50 am, Juha Nieminen wrote:
    > Grizlyk wrote:
    > > Take in account, that the code
    > > {
    > >    Derived* d = new Derived();
    > > ...
    > >    delete d;
    > > }
    > > must not be used by programmers (there are some exceptions)

    >
    >   Well, if that's a bad example, then why cannot a *good* example be
    > used instead? A short piece of good-quality, actual, realistic C++ code,
    > rather than "don't do this, it's just an example". Especially since the
    > example doesn't say so, it just assumes the reader knows.


    Hmm. Readers must not learn any by copy&paste way and realistic code
    will include many C++ properties simultaneously, so it is not easy to
    set emphases in the complete example, not easy to remove all
    properties unrelated to sense of the example.

    I hope you agree that good example must use RAII wrappers to hide
    ownership. So, do you offer to write like this:

    {
    my_own_great_holder<Derived> d=new Derived;

    Base* b = d();
    b->f(65.3);
    d->f(65.3);

    }

    ? I beleive, here people will fall into thinking about "what is
    my_own_great_holder<Derived>".

    If you want to hide all bad examples here, you can write only part of
    code not compileable without external code, like this

    Derived *d=new Derived;
    Base *b = d;
    b->f(65.3);
    d->f(65.3);

    or with auto or static memory, like this

    int main()
    {
    Derived obj;

    Derived *d = &obj;
    Base *b = d;

    b->f(65.3);
    d->f(65.3);
    }

    ===
    grizlyk
     
    Grizlyk, Jan 6, 2009
    #11
  12. Juha Nieminen

    Triple-DES Guest

    On 5 Jan, 07:24, Grizlyk <> wrote:
    > By the way, there is interesting question - does examples of C++ must
    > be most simple or most complete?
    >
    > Take in account, that the code
    >
    > {
    >    Derived* d = new Derived();
    >
    > ...
    >    delete d;
    >
    > }
    >
    > must not be used by programmers (there are some exceptions), you
    > should write like this:
    >
    > {
    > my_own_great_holder<Derived>  d=new Derived;
    > ...
    >
    > }


    I think sometimes you are simply forced to make simplifications that
    affect the correctness of the program, especially in the presence of
    exceptions, because adding all the code to make it exception-safe
    detracts too much from the technique you were trying to demonstrate in
    the first place. At least as long as shared_ptr, lambdas etc. are not
    standardized.
     
    Triple-DES, Jan 12, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Heinz

    Questionable compiler warning

    Thomas Heinz, Jul 8, 2004, in forum: C++
    Replies:
    12
    Views:
    5,828
    Gary Labowitz
    Jul 9, 2004
  2. Rouben Rostamian

    questionable cast

    Rouben Rostamian, Jan 18, 2004, in forum: C Programming
    Replies:
    6
    Views:
    356
    Barry Schwarz
    Jan 19, 2004
  3. Andrew
    Replies:
    2
    Views:
    388
    Pete Becker
    Oct 19, 2007
  4. Aaron Brady

    SQL, lite lite lite

    Aaron Brady, Dec 29, 2008, in forum: Python
    Replies:
    15
    Views:
    773
    Aaron Brady
    Jan 1, 2009
  5. Markus S
    Replies:
    2
    Views:
    382
    Markus S
    Jul 13, 2009
Loading...

Share This Page