From owner-freebsd-hackers Sat Oct 19 05:18:26 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id FAA18794 for hackers-outgoing; Sat, 19 Oct 1996 05:18:26 -0700 (PDT) Received: from hps.sso.wdl.lmco.com (hps.sso.wdl.lmco.com [158.186.22.100]) by freefall.freebsd.org (8.7.5/8.7.3) with SMTP id FAA18784 for ; Sat, 19 Oct 1996 05:18:22 -0700 (PDT) Received: from miles.sso.wdl.lmco.com by hps.sso.wdl.lmco.com (4.1/SSO-4.01-LMCO) id AA10760; Sat, 19 Oct 96 08:17:04 EDT Received: by miles.sso.wdl.lmco.com (4.1/SSO-SUN-2.04) id AA14931; Sat, 19 Oct 96 08:15:02 EDT Date: Sat, 19 Oct 1996 08:15:01 -0400 (EDT) From: Richard Toren X-Sender: rpt@miles To: hackers Subject: Re: C++ question In-Reply-To: <199610172119.PAA20617@rocky.mt.sri.com> Message-Id: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-hackers@FreeBSD.org X-Loop: FreeBSD.org Precedence: bulk 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 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 | ====================================================