From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1d485db3760413be X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-17 16:57:49 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!uunet!sea.uu.net!ash.uu.net!world!news From: Robert A Duff Subject: Re: Compiler default initialization of array types User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Sender: news@world.std.com (Mr Usenet Himself) Message-ID: Date: Thu, 17 Oct 2002 23:57:13 GMT Content-Type: text/plain; charset=us-ascii References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Organization: The World Public Access UNIX, Brookline, MA Xref: archiver1.google.com comp.lang.ada:29886 Date: 2002-10-17T23:57:13+00:00 List-Id: "Matthew Heaney" writes: > "Sebastian" 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, > ) 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