Questionable code example in C++ FAQ Lite?

J

Juha Nieminen

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.)
 
A

alfps

  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
 
J

James Kanze

Consider the code example here:

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.
 
J

Juha Nieminen

James said:
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.
So what's the point?

Do you only read one paragraph at a time when you answer to someone's
post?
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?
 
J

jason.cipriani

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



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



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


  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
 
A

alfps

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
 
L

Labyrinth


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.
 
G

Grizlyk

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

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?
 
J

Juha Nieminen

Grizlyk said:
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.
 
G

Grizlyk

  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
 
T

Triple-DES

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top