comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Compiler default initialization of array types
Date: Thu, 17 Oct 2002 23:57:13 GMT
Date: 2002-10-17T23:57:13+00:00	[thread overview]
Message-ID: <wcc3cr4vcti.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: uqtfi1i12aa730@corp.supernews.com

"Matthew Heaney" <mheaney@on2.com> writes:

> "Sebastian" <sebastian.madunic@avionics.saab.se> wrote in message
> news:aombha$ong$1@newstoo.ericsson.se...
> > I wonder if Ada always assignes Array types with default values?
> 
> Only if the component subtype is default initialized.  Otherwise no.
> 
> For example:
> 
> declare
>    S : String (1 ..10);
> begin
> 
> The array S is NOT initialized.
> 
> However, if we have this:
> 
> type String_Access_Array is
>     array (Positive range <>) of Ada.Strings.Unbounded.String_Access;
> 
> declare
>    S  : String_Access_Array (1 .. 10);
> begin
> 
> Then here array S IS initialized.

Just to clarify: the components of S are initialized because access
types always have a default initial value of null.  For an array of
records, the subcomponents are initialized if the record declares
defaults for its components.  As you said, an array of character or
integers or something like that is not initialized by default.

> > In case this is true; is there some way to suppress this feature?
> 
> As someone already pointed out, you might be able to use pragma Import (Ada,
> <array object>) to suppress the initialization, but I haven't tried this
> with an array object.  Another possibility is to turn off
> default-initialization of the array component subtype.  Is there a pragma to
> do this?  I forget.

No.  The pragma Import(Ada,..) should work for objects.  If it doesn't,
it's a compiler bug.

But there is no pragma on a type to say never default-initialize objects
of that type.  For an array of records, it's no big deal -- just don't
put any defaults on the record components.  But for an array of pointers
(or array of records containing pointers, etc), the compiler will want
to initialize the pointers to null.

I have written code where there were lots of giant arrays of pointers,
inside a record with another component indicating how many are valid.
Something like:

    type T is
        record
            Last: Natural := 0;
            Pointers: Pointer_Array(1..Big_Number);

Typically, objects of type T would use just a few pointers -- like Last
might be 3 or 4, but very unusual for it to be 1000 or 1_000_000.

It turned out that initializing all those unused pointers to null was a
serious performance problem.  So I cheated: I declared the array as an
array of integers (where the integer type was chosen to be the same size
as a pointer), and used unchecked_conversion.

> However, if the array component subtype is controlled, I don't think there's
> any way to turn of controlled initialization.  But I wouldn't think you'd
> want to, either.
> 
> Is the array component subtype scalar?

If so, it has no particular default, so the compiler will typically not
default-initialize.

Unless you use pragma Normalize_Scalars, which attempts to detect
uninitialized variables by default-initializing them to out-of-range
values.

> Perhaps we could help you more if you provided the declarations of the array
> component subtype and array subtype.

True.  It would also help if the OP explained why default-init is
undesirable.  Efficiency?  Interfacing to objects initialized in some
other language, or by hardware?

- Bob



  reply	other threads:[~2002-10-17 23:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-17 12:55 Compiler default initialization of array types Sebastian
2002-10-17 13:10 ` Lutz Donnerhacke
2002-10-17 22:39   ` Peter Richtmyer
2002-10-17 13:42 ` Matthew Heaney
2002-10-17 23:57   ` Robert A Duff [this message]
2002-10-18  9:50     ` Larry Kilgallen
2002-10-18 14:40       ` Robert A Duff
2002-10-18 15:04         ` Larry Kilgallen
2002-10-18 13:36     ` Matthew Heaney
2002-10-18 15:28     ` Wes Groleau
  -- strict thread matches above, loose matches on Subject: below --
2002-10-17 14:23 Grein, Christoph
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox