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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c1071f2bf741de0 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: [Q]: Unchecked conversion Date: 1996/11/10 Message-ID: #1/1 X-Deja-AN: 195712230 references: <01bbcf21$4b32a020$829d6482@joy.ericsson.se> content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-11-10T00:00:00+00:00 List-Id: In article <01bbcf21$4b32a020$829d6482@joy.ericsson.se>, "Jonas Nygren" wrote: >I'm not sure if its portable, but I have used something similar to > > function To_Buffer (S : String) return Buffer is > Temp_Buffer : Buffer(1..S'Length) > for Temp_Buffer use at S'Address; > begin > return Temp_Buffer; > end To_Buffer; In general, you should avoid overlay-style conversions when Unchecked_Conversion will do. Overlays are inherently dangerous because you can unwittingly over-write memory that doesn't belong to you, especially when the overlay-object has default initialization (for example, an access object). The preferred approach is as Bob Dewar suggested: function To_Buffer (S : String) return Buffer is subtype Constrained_String is String (S'Range); subtype Constrained_Buffer is Buffer (S'Range); function To_Constrained_Buffer is new Unchecked_Conversion (Constrained_String, Constrained_Buffer); begin return To_Constrained_Buffer (S); end; This should be portable across all Ada compilers, because the source and target types are both constrained. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant mheaney@ni.net (818) 985-1271