brangdon@cix.co.uk (Dave Harris) wrote in message news:... > ron@sensor.com (Ron Natalie) wrote (abridged): > > int i; // initialize to 0 > > stream >> i; > > auto int i; // recycle auto to mean uninitialized. > > stream >> i; > > In the above case existing code that used the unqualified name would > > work in old and new compilers (with a slight performance hit in the > > new complier), those in the second case would work the same way in > > the old and new compiler (the same way it used to). > A good optimiser could emit the same code in both cases. But only because it is allowed to have special knowledge of std::istream. If you are dealing with a user defined stream, it can't. > The value zero is never read, so never needs to be written. But if operator>>( MyStream&, int& ) is in another translation unit, how can the compiler know this? > The compiler could figure this out either from the source (eg if > stream is from a template) or from knowing the semantics of the std > library (if stream is from the std). Cases where zero- initialisation > cannot be optimised away are fairly rare - although sometimes > important when they do arise. > If we actually need zeros, it may be more efficient to let the > compiler provide them. For example, with an instance of a deep class > hierarchy, the compiler can zero-initialise all of the instance > variables with a single memset(), instead of every class needing its > own set of assignments. > Indeed, it can make sense to push the nulling even further back, and > have a "nulled heap". Then the system can do the work of writing nulls > during its idle time. I've long thought that a useful optimization for a compiler would be to work out the image of the local variables after initialization, generate a static block with the same image, and replace the initialization code by a memcpy. For C, I'm sure this would help; for C++, where initialisation in normally in the form of constructors (defined in other translation units), I'm less convinced. -- James Kanze GABI Software mailto:kanze@gabi-soft.fr Conseils en informatique orient�e objet/ Beratung in objektorientierter Datenverarbeitung 11 rue de Rambouillet, 78460 Chevreuse, France, T�l. : +33 (0)1 30 23 45 16 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]