From: Gib Bogle on
Should one rely on allocated arrays being initialized to zero?
From: Uno on
Gib Bogle wrote:
> Should one rely on allocated arrays being initialized to zero?

I brushed a thick layer of dust off MR&C and was largely unsuccessful in
a similar endeavor.

What does one need to do to set all of an object's attributes to zero?
--
Uno
From: Richard Maine on
Gib Bogle <g.bogle(a)auckland.no.spam.ac.nz> wrote:

> Should one rely on allocated arrays being initialized to zero?

No. Nowhere does the standard even hint at such a thing.

Even on compilers that initialize static arrays to zero, either by
happenstance or as a "feature", your odds of it happening to act that
way for allocated arrays are probably low. The compiler would likely
have to actively insert code to zero the array because it likely is
using memory that was formerly used for something else.

I recommend against relying on such nonstandard features at all. Code
that uses such features quite commonly needs to be fixed later for one
reason or other, and it is sometimes a lot of work to fix - hugely more
work than it would have been to just explicitly initialize things
properly in the first place. I often take such failure to explicitly
initialize as indicative of failure to think about what the program
needs, which sometimes is more than just initializing variables to zero.

In the case of allocated arrays, odds are fairly high that it just won't
work at all.

One thing that does happen when an array is allocated is default
initialization. But default initialization applies only to derived
types. It is specified in the type definition.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Richard Maine on
Uno <merrilljensen(a)q.com> wrote:

> What does one need to do to set all of an object's attributes to zero?

That doesn't even make sense. Most attributes aren't numeric at all and
aren't "settable" during execution. I can only suppose you mean
something other than "attribute", but I can't guess what it would be.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: m_b_metcalf on
On Apr 20, 8:26 am, Uno <merrilljen...(a)q.com> wrote:
> I brushed a thick layer of dust off MR&C and was largely unsuccessful in
>   a similar endeavor.
>
Then might I draw your attention to Section 3.3, 'Defined and
undefined variables'.

Regards,

Mike Metcalf