Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!unmvax!tut.cis.ohio-state.edu!rutgers!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: cfront feature or bug? Summary: Don't forget ?: Message-ID: <175@mole-end.UUCP> Date: 7 May 89 08:29:10 GMT References: Distribution: comp.lang.c++ Organization: mole-end--private system. admin: mole-end!newtnews Lines: 51 > I have just spent an hour trying to find out why my Vector class > implementation was core dumping. ... _> class X { _> int i; _> public: _> X() {}; _> X(int n) { i = n; } _> void times_two () {i *= 2;} _> }; _> void dummy (int k) { _> int i; _> if (k > 3) _> X* t = new X[10]; _> else _> X* t = new X[20]; _> t.times_two(); _> } > At a first glance, this code seems to be OK. Eh? Try X* t = ( k > 3 ) ? new X[ 10 ] : new X[ 20 ] ; > ... An even worse problem ... _> void dummy (int k) { _> int i; _> if (k > 3) { _> ::: // some code here _> X* t = new X[10]; // <== Wrong scope, fershure ... _> ::: // more code here _> } _> else _> X* t = new X[20]; _> t.times_two(); _> } > My questions are: 1) Is this a bug in cfront? > 2) If it is a bug, is it fixed in 2.0? More likely, it's a weakness in the language. This is one of those places where you really ought to declare the variable and initialize it later, I think. -- (This man's opinions are his own.) From mole-end Mark Terribile