[net.lang.c++] Cornell Bug Report #0 of 4

gdykes@batcomputer.TN.CORNELL.EDU (Gene Dykes) (06/19/86)

	Here at Cornell, it appears that C++ will play a major role in 
the development of software that will be shared over the campus-wide
network (TheoryNet).  I have used it to write a menu manager, and it
sure made the task easier.  I love it!   However...

	I have a few bugs to report that I don't think have been posted
yet (to be sent in three separate articles), a few bugs that are probably
well known (to be described at the end of this article) and a gripe or
two (to be discussed now).

Gripe #1:
The form() function only takes 8 arguments, which is bad, and that fact
is undocumented (I think), which is worse.  The fix is trivial, but I
did spend several hours trying to figure out why some of my output was
mangled until I discovered the problem.

Gripe #2:
It seems that I am always needing to declare a function in one class
as the friend of another class, while also needing to declare a function
in the second class as a friend in the first.  C++ squawks because it
needs to have the class defined before it can have a function in it
declared as a friend in another class.  Needless to say, it is impossible
to declare each class before the other.  So...
	1) Does this indicate lousy programming practices on my part?
	2) Do lots of other people also run into this?
	3) Is there a way to do this that I haven't figured out?
	4) Is this something that can't be helped or is a fix on the way?

- - - - - - - - - - - -
Probable well-known bugs
- - - - - - - - - - - -
% cat bug.c
#include <CC/stream.h>

main ()
{
cerr << "works\n" ;
}

% CC bug.c -o bug 
CC  bug.c:
cc   bug..c -o bug -lC
cc: warning: -o ignored when -c or -S used

% bug 
works

% CC -c bug.c 
CC  bug.c:
cc  -c  bug..c -lC

% cc bug.o -o bug -lC 
% bug 
Bus error (core dumped)

- - - - - - - - - - - - -

Two problems are revealed by this simple example:

	1) a program cannot be compiled by CC and loaded by cc
	2) the ridiculous warning that always appears during a CC load

I assume these two problems are well known.  If not, there must be something
wrong here, and I would appreciate somebody letting me know what they are...
-- 
Gene Dykes, 120 Rand Hall, Cornell U., Ithaca, NY 14853 (607)255-6713
{ihnp4,decvax,allegra,vax135}!cornell!batcomputer!gdykes

cmc@rlvd.UUCP (06/24/86)

In article <498@batcomputer.TN.CORNELL.EDU> gdykes@batcomputer.UUCP (Gene Dykes) writes:
>...
>% CC -c bug.c 
>CC  bug.c:
>cc  -c  bug..c -lC
>
>% cc bug.o -o bug -lC 
>% bug 
>Bus error (core dumped)
>
>...
>
>	1) a program cannot be compiled by CC and loaded by cc
>

The problem arises if your C++ program uses static constructors and/or
destructors. A big problem with such programs is that the compiler
has to insert a call to a special routine which calls all of the constructors
*before* the program starts executing for proper. This cannot be done
during the initial compiling stage as the compiler cannot know what
constructors will be called from other modules, e.g. the constructors
for the standard streams (cout, cin, cerr) in libC.a. The solution used
depends on whether you are working on a system with libld.a. If you are,
a program called patch is used to build a list of pointers to static
constructors and patch it into the executable, On other systems nm(1)
is run on the executable and the output piped into a special program
called munch. This spots symbols which are static constructors and generates
a C file which is then compiled and the whole executable is relinked (now
you see why compiling/linking can take an age!)

The reason your program bus errored is that the stream package is not
being initialised.

			Chris.

mat@mtx5a.UUCP (m.terribile) (06/25/86)

> Gripe #2:
> It seems that I am always needing to declare a function in one class
> as the friend of another class, while also needing to declare a function
> in the second class as a friend in the first.  C++ squawks because it
> needs to have the class defined before it can have a function in it
> declared as a friend in another class.  Needless to say, it is impossible
> to declare each class before the other.  So...
> 	1) Does this indicate lousy programming practices on my part?
> 	2) Do lots of other people also run into this?
> 	3) Is there a way to do this that I haven't figured out?
> 	4) Is this something that can't be helped or is a fix on the way?

My personal opinions:

	No, it's not lousy programming practice.  I just missed it, and I
thought it was legal.  In fact it's not -- the example below fails as
indicated, and I think that it addresses a rather common problem.

	Is there a way around it?  Yes.  It's ugly as sin and not half as
much fun.  You should be able to declare the entire second class as a friend
of the first.  ``friend class c2;''

	As far is there being a possible fix ... I think that we have to
ask Bjarne.  I'd like to see it work in the obvious way.  If it can't be
done for all member functions, then I'd like to see it at least work for
constructors (see the example).  I suspect that the fix will involve adding
another hyperfine level of sophistication to cfront, but I don't know, not
having studied cfront.

class	c2;	// c1 and c2 are alternate representations ... one is packed
		// for storage and one is unpacked for reasonable coding usage.
		// Much stuff omitted ...

class	c1
{
  public:
	c1( c2 );		// This is legal.
	friend	c2::c2( c1 );	// This is not!
};

class	c2
{
  public:
	c2( c1 );
	friend	c1::c1( c2 );
};
-- 

	from Mole End			Mark Terribile
		(scrape .. dig )	mtx5b!mat
					(Please mail to mtx5b!mat, NOT mtx5a!
						mat, or to mtx5a!mtx5b!mat)
    ,..      .,,       ,,,   ..,***_*.