
From: Roland Schwarz
Martin Bonner wrote:
You may be able to do a little better. The standard says "The storage for objects with static storage duration (3.7.1) shall be zeroinitialized (8.5) before any other initialization takes place." What is more, most normal operating systems give programs pages of memory that are zero-initialized, so compilers would have to do work to avoid this.
Sorry, I do not understand "so compilers would have to do work to avoid this". Could you please try to state it with different wording?
It is a requirement of the standard that static objects are zero initialized before the constructor runs. Compilers sometimes fail to implement the standard for various reasons (backwards compatibility, bugs, etc). What I am saying is that the natural way of implementing static objects on most implementations will automatically meet this requirement of the standard - so it is fairly safe to rely on.
I think that means you can declare constructors and STILL have the object well behaved.
Do you mean: the object might be already zero initialized before the ctor runs? If yes: this is very dangerous to rely on!
1) "Might": the standard does not require it.
But the standard DOES require it for STATIC objects (which is what you were worrying about the order of initialization for).
2) E.g.: MSVC initializes memory to "CDCDCDCDCDCD...." in debugging builds, so there is at least one prominent case where the assumption is false.
It only initializes heap memory to CD - not "objects with static storage duration". -- Martin Bonner Martin.Bonner@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB24 6WZ, ENGLAND Tel: +44 (0)1223 203894