Kenneth Bull wrote:
2009/9/8 Mason Wheeler :
Your assertion that most initialization values should be zero or NULL
is completely f’n retarded. It is critical to initialize values
yourself.
This is true, you should be initializing variables yourself, but
having a sensible default assigned by the constructors of the various
types you’re working with helps. Not zeroing everything through some
backend compiler magic, but using well designed type classes that do
it for you. Even int and double have constructors, which do assign a
zero value by default. The compiler just doesn’t always call these
constructors on basic types when you might expect it to.
Isn’t it true that in C++, all uninitialized data members are implicitly
0? Which is to say that it’s
not by some ‘backend compiler magic’ that an uninitialized pointer member
of some class is
already NULL before initialization, but instead through compliance to the
language itself.
To make it extra clear for those who don’t get it: Zero is often not the
right ultimate value to initialize your references to when you create an
object. But it’s the only starting point that makes sense, because
things can go wrong while creating an object, and C++'s object model
not zeroing object memory before the constructor runs is a serious
defect.
I’m sure you meant ‘pointer’ rather than ‘reference’ in the preceding.
On the other hand, there’s often a fair amount of the object that you want
to initialize to 0, especially non-object members. In this case, zeroing
the memory beforehand saves you a lot of work.
“Zeroing memory beforehand” would also be time consuming for large
types and arrays. So would calling constructors. Also, if you’re
using pointers, the compiler can’t really initialize the pointer’s
target, only the pointer itself.
Everybody has to pay the consequences for their design decisions, if you
really have a large array that
has to be zeroed, I’m assuming that you’ve examined alternative methods
and concluded that it has to
be done, and that nobody is going to do it for you.