Date: Sat, 19 Oct 1996 08:15:01 -0400 (EDT) From: Richard Toren <rpt@sso.wdl.lmco.com> To: hackers <hackers@FreeBSD.org> Subject: Re: C++ question Message-ID: <Pine.SUN.3.91.961019081145.14917A-100000@miles> In-Reply-To: <199610172119.PAA20617@rocky.mt.sri.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Here is another anomoly between g++ and Sun C++ 4.1
Here is one from the obscure pages of the ARM (I hope).
What action is to be taken when an object is passed by value to a
function that uses 'varargs'. Is the copy ctor called? is the dtor
also supposed to be called?
Who did it correctly ??
The following situation was noticed due to a type (rather common) when
attempting to print the address of an object, and the '&' was left
off. Unfortunately, the real object was a reference counted pointer. I
noticed that the Sun 4.1 C++ compiler used the supplied copy ctor, but
never called the dtor. This left the reference count one too high.
Some experimenting with G++ and an old CenterLine compiler showed that
both of these made copies in the stack without calling the copy ctor
(probably just the compilers byte-wise copy) and then passed the
adress rather than the object itself.
Sun C++ 4.1 gave no warnings (even with +w).
CenterLine gave no warnings, but did not call the user copy ctor.
G++ gave a warning
(missingDtor.C:39: warning: cannot pass objects
of type `Apple' through `...'
G++ output looks like:
copyCtorbug - called with 1 args
empty ctor - refCount=1
The real address of myfruit is 0xefbfd6b4
Addr of myfruit is 0xefbfd6ac
refCount = 1
dtor - refCount=0
copyCtorbug - called with 2 args
empty ctor - refCount=1
The real address of myfruit is 0xefbfd6ac
Addr of myfruit is 0xefbfd6ac
refCount = 1
dtor - refCount=0
CenterLine gave essentialy the same result.
Sun compiler gave:
copyCtorbug - called with 1 args
empty ctor - refCount=1
The real address of myfruit is 0xeffff1e0
copy ctor - refCount = 2 // !!!!!!!!!!
Addr of myfruit is 0xeffff1d8
refCount = 2
dtor - refCount=1 // wrong ???
copyCtorbug - called with 2 args
empty ctor - refCount=1
The real address of myfruit is 0xeffff1d8
Addr of myfruit is 0xeffff1d8
refCount = 1
dtor - refCount=0
//
// test missing dtor with call to vararg
//
#include <stdio.h>
int refCount = 0;
class Apple {
int somevalue;
public:
Apple ( ):somevalue(100) {
refCount ++;
printf("empty ctor - refCount=%d\n",refCount);
}
Apple(const Apple& rs) {
refCount++;
printf("copy ctor - refCount=%d\n",refCount);
somevalue = rs.somevalue;
}
~Apple ( ) {
refCount--;
printf("dtor - refCount=%d\n",refCount);
}
};
main (int argc, char **argv )
{
printf("\n\n\ncopyCtorbug - called with %d args\n",argc);
Apple myfruit;
Apple *mfp;
mfp = &myfruit;
printf("The real address of myfruit is %#x\n",mfp);
if (argc==1) {
printf("Addr of myfruit is %#x\n", myfruit);
}else {
printf("Addr of myfruit is %#x\n", &myfruit);
}
printf("refCount = %d\n",refCount);
return 0;
}
====================================================
Rip Toren | The bad news is that C++ is not an object-oriented |
rpt@sso.wdl.lmco.com | programming language. .... The good news is that |
| C++ supports object-oriented programming. |
| C++ Programming & Fundamental Concepts |
| by Anderson & Heinze |
====================================================
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SUN.3.91.961019081145.14917A-100000>
