Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Questionable code example in C++ FAQ Lite?

0 views
Skip to first unread message

Juha Nieminen

unread,
Dec 28, 2008, 4:26:19 AM12/28/08
to
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.)

alfps

unread,
Dec 28, 2008, 5:26:45 AM12/28/08
to cl...@parashift.com
On 28 Des, 10:26, Juha Nieminen <nos...@thanks.invalid> 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

James Kanze

unread,
Dec 28, 2008, 8:47:25 AM12/28/08
to
On Dec 28, 10:26 am, Juha Nieminen <nos...@thanks.invalid> 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:james...@gmail.com
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

Juha Nieminen

unread,
Dec 28, 2008, 10:07:26 AM12/28/08
to
James Kanze wrote:
> On Dec 28, 10:26 am, Juha Nieminen <nos...@thanks.invalid> 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?

jason.c...@gmail.com

unread,
Dec 28, 2008, 8:35:50 PM12/28/08
to

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

alfps

unread,
Dec 29, 2008, 1:07:50 AM12/29/08
to
On 28 Des, 11:26, alfps <alf.p.steinb...@gmail.com> 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".

jason.c...@gmail.com

unread,
Jan 2, 2009, 9:00:32 PM1/2/09
to

Labyrinth

unread,
Jan 2, 2009, 10:15:18 PM1/2/09
to
In article <c939fa27-2193-4712...@l39g2000yqn.googlegroups.com>, "jason.c...@gmail.com" <jason.c...@gmail.com> wrote:

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.

Grizlyk

unread,
Jan 5, 2009, 1:24:25 AM1/5/09
to
> On Dec 28 2008, 4:47 pm, James Kanze wrote:
> > On Dec 28, 10:26 am, Juha Nieminen <nos...@thanks.invalid> 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?

Juha Nieminen

unread,
Jan 5, 2009, 7:50:46 PM1/5/09
to
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.

Grizlyk

unread,
Jan 5, 2009, 9:16:10 PM1/5/09
to
> 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

Triple-DES

unread,
Jan 12, 2009, 8:06:17 AM1/12/09
to
On 5 Jan, 07:24, Grizlyk <grizl...@yandex.ru> 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.

0 new messages