From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,85e23020c171db90 X-Google-Attributes: gidfee84,public X-Google-ArrivalTime: 1994-09-10 11:14:47 PST Path: nntp.gmd.de!xlink.net!slsv6bt!slbh01.bln.sel.alcatel.de!rcvie!Austria.EU.net!newsfeed.ACO.net!paladin.american.edu!gatech!swrinde!sgiblab!gatekeeper.us.oracle.com!decwrl!pa.dec.com!virrus.zko.dec.com!diewald From: diewald@virrus.zko.dec.com (Jeff Diewald) Newsgroups: comp.unix.osf.osf1 Subject: Re: DEC cxx BUGc Date: 9 Sep 1994 19:41:44 GMT Organization: Digital Equipment Corporation Distribution: world Message-ID: <34qdpo$9m@usenet.pa.dec.com> References: <347n7u$l7u@cismsun.univ-lyon1.fr> Reply-To: diewald@virrus.zko.dec.com (Jeff Diewald) NNTP-Posting-Host: virrus.zko.dec.com Keywords: DEC cxx BUG X-Newsreader: mxrn 6.18-9 In article <347n7u$l7u@cismsun.univ-lyon1.fr>, buffat@europe.mecaflu.ec-lyon.fr (Buffat Marc) writes: |> |>I am using the C++ compiler of DEC cxx with OSF 2.0, |> DEC C++ V1.3A-0 for DEC OSF/1 (Alpha) |>and I try to declare and use |>an array of class object (with no default constructor). I have some |>problem (i.e. a Segmentation fault) and I want to know if it is because |>I am doing wrong or because of a compiler bug |>Here is the example: |> |>#include |>#include |>class A { |>private: |> int N; |> float *tab; |>public: |> A(int n):N(n) { tab=new float[N]; } |> ~A() { delete tab; } |> float & operator()(int i) { return tab[i]; } |> friend ostream& operator<<(ostream &s,A &X); |>}; . . . |> Marc |> |>PS: its a compiler BUG, because the program work on a SUN with cfront v2.0 Marc, This is definitely not a compiler bug. Your program has several common memory allocation and deallocation problems in it. The reason it works on another system with another compiler is sheer accident. In particular, you're implicitly copying objects with a bitwise copy of the class. This means that you end up with more than one copy of the pointer to the array of floats. That's OK the *first* time you call delete (for one copy), but it's definitely a bad idea for any succeeding calls (like deleting a copy). Get a copy of Scott Meyer's excellent "Effective C++; 50 Specific Ways to Improve Your Programs and Designs" (Addison Wesley Publishers, ISBN 0-201-56534-9) and look at #11. DECladebug makes it easy to see what's going on: asaab[jeff 118]> $DECLADEBUG bug Welcome to the DECladebug Debugger Version 3.0-14 ------------------ object file name: bug Reading symbolic information ...done (decladebug) when in ~A {print this; print *this;} [#1: when in A::~A(void) { print this; print *this; ; ; } ] (decladebug) run [1] when [A::~A(void):9 0x12000315c] 0x11ffff3a0 class { N = 10; tab = 0x14000d030; } [1] when [A::~A(void):9 0x12000315c] 0x11ffff390 class { N = 10; tab = 0x14000d060; } X= 0 1 2 3 4 5 6 7 8 9 0 0 4 6 8 10 12 14 16 18 0 0 0 0 12 15 18 21 24 27 [1] when [A::~A(void):9 0x12000315c] 0x11ffff340 class { N = 10; tab = 0x14000d060; } [1] when [A::~A(void):9 0x12000315c] 0x11ffff330 class { N = 10; tab = 0x14000d090; } X= 0 1 2 3 4 5 6 7 8 9 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 Y= 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 [1] when [A::~A(void):9 0x12000315c] 0x11ffff378 class { N = 10; tab = 0x14000d090; } Thread received signal SEGV stopped at [free:797 0x12000ea2c] (Cannot find source file ../../../../../../src/usr/ccs/lib/libc/malloc.c) (decladebug) where >0 0x12000ea2c in free() ../../../../../../src/usr/ccs/lib/libc/malloc.c:797 #1 0x120005478 in operator delete() delete.cxx:27 #2 0x120003174 in ((A*)0x11ffff378)->~A() bug.C:9 #3 0x120003a80 in main() bug.C:34 (decladebug) Note the value of tab? See any other places it's been deleted? See how we've deleted the array at 0x14000d060 twice without a SEGV - just by luck. It's the second time we try to free something twice that all heck breaks loose. It's sheer luck that it worked the first time and not the second time. I've seen programs run like this for long periods of time before dying an ugly death. Or, you can always just define a constructor of the form: A(const A& copy) { cout << "Copy A! Bad idea!" << endl; N=copy.N; tab=copy.tab; // Wrong, but what the default constructor // is doing now... } Then run your program again... See what's happening now? Another minor nit - see item #5 in the Meyers book. If you allocate with a [], you should delete with a []. Thus, a more correct destructor would be "delete [] tab;" ----------------------------------------------------------------------------- Jeff Diewald EASYNET: VIRRUS::DIEWALD DECladebug group INTERNET: diewald@virrus.zko.dec.com Digital Equipment Corp. Note: I don't read every article in this group. If you have debugger questions, I may be able to get you an answer. You're much more likely to get an answer if you put "Debug" somewhere in your subject line.