Am 23.01.2011 01:33, schrieb ThorstenB:
> [...]
> Uh, ugly! Why (and where?) are these members only initialized when the first
> instance is created? Or are they never initialized and it's just because
> memory often happens to be 0 when the first instance is created?
IIRC, gcc has a default initialization routine that sets new variables
to zero unless they are explicitly initialized like
int a=1;
If the initial value in this example is not zero, it has to be stored in
the executable, increasing its size. When initializing to zero, the zero
is not stored in the executable, it seems.
I've struck up a little theory of what might happen, though I have no
definite evidence and none of the following should be taken for a fact:
- in the prologue of the main function, the stack is initialized to zero
(this even seems not to be the case when using -O3...)
- gcc default initialization means putting the variable on the stack
which is assumed to be zero because of the above
- after a reset, new instances will not be created in fresh stack
memory, but in sections that were freed when deleting the old instances
- these sections were indeed freed but not zeroed
- the new instances contain pieces of the leftovers of the previous
instances
> Definitely a good idea! Uninitialized members easily cause random (=very
> ugly) problems - and such issues are a pain to debug. I'm strongly in favour
> of initializing all variables/members in the constructor. And especially so
> in a case like this, where few objects are created: even if initializing a
> certain member wouldn't be necessary, it doesn't cost us much except a few
> bytes of code - but no run-time performance at all.
Though I'd also be in favor of this, it might be quite a lot of code to
be added. Maybe it is an alternative to begin every constructor with
something like
memset(this, 0, sizeof(this))
Maybe there is also a way to re-initialize freed stack parts, provided
the above theory holds true.
>> Can anybody point me to some documentation about how
>> initialization of class members is supposed to happen the right way?
> Not quite sure what you mean with "the right way".
I was looking for something explaining when it is safe to rely on gcc's
default initialization, if ever. Details on the difference between the
member initialization list and assignments in the constructor code might
also be helpful.
btw, all of the above is specific to gcc. Have the NaN issues been
observed with other compilers?
Best regards,
Andreas
------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Flightgear-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/flightgear-devel