|
By Ali Cehreli
I've been ignoring this issue since the first time I received the
"deletion of pointer to incomplete type" warning from the
compiler. The expensive approach I used to take to make the warning
go away was to include the class header file to make the class
definition visible to the compiler. [0]
There are many benefits of using smart pointers instead of plain
pointers. Memory leaks are avoided and the object lifetimes are well
defined because the objects are deleted when the smart pointers that
own them go out of scope. In addition, using smart pointers is
necessary to write exception-safe code. The constructors of classes
that own more than one resource cannot release all of those
resources when exceptions are thrown unless the resource management
is moved to separate classes like smart pointers. [1]
There are many smart pointer implementations that are more
suitable to certain tasks than auto_ptr but
I will use auto_ptr here because
it's the only smart pointer included in the C++ standard library.
[2]
The following class uses auto_ptr to
manage its only resource. Unfortunately though, when the class
header is included by a user, the compiler will emit a diagnostic
pointing to the fact that the destructor of the incomplete type CImpl
is being called.
// c.h - The interface
#include <memory>
#include <string>
class CImpl;// forward dcl
class C
{
std::auto_ptr<CImpl> p_;
std::string s;
private: // unimplemented
C(C const &);
C& operator=(C const &);
public: // interface
C(const std::string& s);
};
Note that in addition to the absence of the definitions of the
copy constructor and the assignment operator, the destructor of the
class is not declared. According to the C++ standard, if the
destructor is not explicitly defined by the author, the compiler is
required to define a destructor that calls the destructors of all of
the data members. [3]
Assume that ctest.cpp, a user of
class C, includes c.h. Here is what
the compiler generated C destructor would look like in that
translation unit:
// ctest.cpp: class C user
// Warning: pseudo code!
inline C::~C()
p_.~std::auto_ptr<CImpl>()
When we replace the single line above with the inline definition
of the auto_ptr destructor, the
compiler generated destructor actually looks like this:
// p_ is variable defined
// in std::auto_ptr
inline
C::~C() { delete p_; }
Deleting the object is indeed what auto_ptr
is responsible for. Now if we go one step further and
decompose the delete statement, we arrive at the following code:
inline C::~C()
{
// call destructor of
// incomplete type
p_->~CImpl();
// some lib function
// that releases memory
__release__(p_);
}
The problem is that the compiler generated destructor needs to
call the destructor of an incomplete type. When faced with this
situation, I used to include the header file of the incomplete class
to make the compiler happy. Still, I was thinking how unfortunate it
was that we couldn't replace plain pointers with auto_ptrs
without increasing the compile time project file dependencies. [4]
This issue came up during a study group [5] where Allan
questioned Herb Sutter's recommendation of using auto_ptr
in the compiler firewall idiom. Besides the arguments about
the suitability of auto_ptr in that
idiom, there was another interesting question: how was Herb able to
use an auto_ptr to an incomplete
type?
The answer is too easy to justify ignoring it for years: Even
though it is not necessary, declare the destructor of C, and provide
an empty implementation in the class implementation file:
// c.cpp
#include "c.h"
#include <iostream>
using namespace std;
C::C(const string& s_)
: s(s_),
p_(new Cimpl(s_))
{
cerr << "C::C() for "
<< s << endl;
}
// define empty dtor
C::~C() {}
Now the compiler sees the declaration of the C destructor and
does not generate it implicitly. As a result, the user code never
directly calls the destructor of CImpl.
As a user of CImpl, c.cpp
already includes cimpl.h and
has access to CImpl's definition. In
fact, in the case of the compiler firewall idiom, c.cpp
is the only intended user of cimpl.h.
Also note that defining the empty C destructor in the header file
does not solve the problem. As Wayne put it nicely, it is where the
destructor is defined that matters—it must be in the
implementation file.
[Ed.—the listings for this example are on our web site, http://www.accu-usa.org
]
References:
[0] See Alan Griffiths' article "Ending with the grin"
at http://www.octopull.demon.co.uk/arglib/TheGrin.html
for more about this problem and solutions.
[1] Herb Sutter, "Exceptional C++," Addison Wesley,
ISBN 0-201-61562-2
[2] See http://www.boost.org
for other smart pointer implementations.
[3] ISO/IEC 14882, Programming Languages - C++, Section 12.4
Destructors
[4] See John Lakos' "Large Scale C++ Software Design"
from Addison Wesley for issues around project file dependencies.
[5] The members of that meeting were Ali Cehreli, Allan Kelly,
Walter Vannini, and Wayne Vucenic.
By Reg. Charney
I am a strong supporter of Open Source software. Thus, for a new
application that I was developing, I hope to do it using Open Source
tools. Since this project involves new tools and technology in which
I was not yet expert, I needed to do a lot of investigation. This
involved downloading a large number of packages to check them out.
The downloaded packages came in many flavors: tar balls (zipped tar
files), RPMs, and self-extracting files.
The first thing that was noticeable was that except for RPMs,
none of the packages had similar installation conventions. There
were README, Readme, make, shell scripts and INSTALL files. Some
packages came with documentation, but most did not. A particularly
egregious example was IBM’s DB2 download. After downloading 325MB,
I found that there was no documentation of any kind.
Given some documentation, packages seldom installed cleanly. One
package I was very interested in, Ariadne, was uninstallable.
Under Linux there are RPMs, and Debian’s .DEB file format.
Unfortunately, many packages fail to use these formats.
I have examined over a dozen packages recently. From my
experience, I can happily report that when I was able to install
them and the documentation existed, they worked very well and the
effort was worth it.
Based on my experience, for those of you who want to get involved
in the Open Source movement, one of the best places to start is to
assist developers with their installation procedures.
With the downturn, some of our friends and valued contributors
are leaving the Valley. This week, Allan Kelly, the author of this
month’s technical article is sailing back to the U.K. We wish him
the best of luck and happiness.
Software Craftsmanship by
Pete McBreen, Addison-Wesley, ISBN 0-201-73386-2
I like concise books. I read this one over a weekend and a few
days of commuting on the 101 express bus to Palo Alto. McBreen
comments on the ofttimes ineffective software development process.
He explores the term “software engineering” and its common
practice: the waterfall model/cycle, the resulting team/corporate
structure and its shortcomings—leading to expensive software,
sometimes buggy and late. The author gives ample references, both to
classic works and online material.
—Oluf Nissen
[This comes via the free-sklyarov mailing list to which I
subscribe—Ed.]
For years, the geek community has been at the wrong end of the
War on Piracy waged by Hollywood lawyers. The situation could
change, however, with the unveiling of a secret weapon—”The
First Church Of Digital Grepping”.
This newly created church argues that copying digital information
is a form of religious worship. As such, it’s protected in the US
by the freedom of religion clause in the First Amendment.
“Rock beats scissors. And Free Exercise of Religion beats
Digital Millennium Copyright Act™. Ha ha, suckers!” said the
church’s High Priest.
Chapter 16, Verse 256 of the Sacred Readme of the First Church Of
Digital Grepping states:
On the first day, the Great Programmer created a new text file
and the Universe was born.
The Great Programmer flexed his fingers, started hacking, and
entered Deep Hack Mode. First He wrote universe.c. Then sys/laws_of_physics.h
and universal_constants.h.
The Great Programmer continued his Hacking Binge into the second
day with sol.c, which begat terra.c, which begat land_and_sea.c,
which laid the foundation for the creation of life.c.
On the third day, He gazed upon his Program and saw that it was
good. More he produced: prokaryotes.c, eukaryotes.c, sys/dna.h,
invertebrates.c, vertebrates.c.
On the fourth day, the Great Programmer, against his better
judgement, coded mankind.c.
On the fifth day, He compiled his work, and received 1,024
errors.
On the sixth day, He debugged.
On the seventh day, He continued to debug. Rest is for the weak.
On the eight day, the debugging continued. Only 128 compiler
warnings did He now receive.
On the ninth day, the program compiled correctly. Upon execution,
it immediately core-dumped.
On the tenth day, The Great Programmer debugged.
On the eleventh day, He debugged.
On the twelfth day, He waved a dead chicken, but the Great
Program continued to segfault.
On the thirteenth day, He discovered the fatal flaw, a misplaced
comma He did find. And then int main()
executed, and the Big Bang did occur.
Then the Great Programmer leaned back in his executive chair, and
gazed upon the newborn Universe.
And frowned. He knew those sentient humans would be a problem.
Even after He had sweated over a hot terminal for thirteen days,
those humans were ungrateful. They called their place of existence
the “Universe”, not the “Great Programmer/Universe”.
On the fourteenth day, he decided to take action. He would send
these humans The Meaning Of Life, and soon the world would worship
Him and his Hacking Skills.
He did just that. He inspired a certain human to produce a work
of art which includes His message, The Meaning Of Life. Eventually
the humans would discover the .plan of the Great Programmer hidden
in a certain work of art and all would be well.
The Sacred Readme is a tad vague, but the church's High Priest
believes that “The Meaning Of Life” is encoded in either a
popular song, or a Hollywood movie, or an Adobe e-book.
“If only we could figure out which ‘work of art’ the Sacred
Readme refers to, and then grep through the binary representation to
extract the divine message,” the High Priest explains.
The mission of the church is to make digital copies of every
music CD, every movie DVD, and every printed book and then grep the
digital version for any tell-tale signs of ‘The Meaning Of Life’.”
“Our church cannot function if the DMCA prohibits us from
making copies as part of the Fair Use Doctrine. We worship the Great
Programmer by trying to discover His secret message. Why should we
put the profits of Big Evil Corporations above the search for The
Meaning Of Life?”
Of course, the MPAA, RIAA, DVD-CCA, BSA, and other groups see
things slightly differently.
“This is all bull,” said a MPAA spokesperson. “We didn't
buy a slate of Congressmen to get the DMCA passed just so some fake
parody religion could claim a bogus exemption!”
An investigator for Oracle discovered a hand-written copy of the
Sacred Readme while rummaging through the High Priest's trash cans.
The P.I. believes that the holy document was actually written last
Wednesday when the High Priest had a little too much to drink.
The founder of the church stands his ground, however. “I wasn't
drunk last Wednesday,” he argues, “I was busy trying to find the
divine message within a copy of ‘Star Trek XXIII: We Promise This
Movie Doesn't Include Any Annoying Characters Like Jar Jar Binks’
on my big-screen projection TV. Needless to say, I came up empty.”
The judges in the California Sixth District Court of Appeals were
all unavailable for comment at press time.
—Humorix: Linux and Open Source(nontm) on a lighter note
References:
By Reg Charney
For months, we have been looking for meaningful changes in the
job market in the Valley. Unfortunately, no significant changes have
occurred. Using our usual sources of data, there is nothing new that
we can say. However, Ali, our usual columnist and I decided to look
at different sources of economic data.
Advertising in printed media is also a good indicator of economic
health. However, it usual it is a trailing indicator. Part of this
is due to the fact that most ads have to be placed in magazines 3-6
months ahead of time. Nevertheless, it is interesting to see what
happened to some of the major magazines in the IT arena. I have kept
most of my copies of PC Magazine over the last 2+ years. The same is
true of my Linux Journal copies. I was interested in finding out the
relative changes in both magazines over the last two years.

In Figure #1, we see the decrease in magazine size for Linux
Journal. The average decrease from 2000 to 2001 is 25.1% while the
median decrease is 34.8%. During the same time period, for PC
Magazine the average decreased between 2000 and 2001 was 17.6% and
the median decrease was 18.0%.

Another way of says this is that the Linux community has suffered
more from the down turn than the general PC industry.
|