From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 13 Aug 93 12:48:35 GMT From: cis.ohio-state.edu!math.ohio-state.edu!magnus.acs.ohio-state.edu!csn!news .den.mmc.com!iplmail!alcyone!rgilbert@ucbvax.Berkeley.EDU (Bob Gilbert) Subject: Data Overlays Message-ID: <1993Aug13.124835.18422@iplmail.orl.mmc.com> List-Id: Thanks to those who replied to my earlier post about exceptions occuring in the declarative region. Now I've got another one: Many times I have wanted to create two different types to look at the same object. An example might be a record structure which is to be output via some DMA device which expects the data to look like an array of bytes. Anyway I've come up with two different methods to implement this. Method 1 : Use an access type of one of the types and assign the address (using unchecked conversion) of the object declare of the other type. type SAMPLE_REC_TYPE is record -- Size is Word_1 : INTEGER; -- 32 + Word_2 : SOME_32_BIT_TYPE; -- 32 + Word_3 : SOME_OTHER_32_BIT_TYPE; -- 32 + Word_4 : MORE_32_BIT_STUFF; -- 32 + Word_5 : A_16_BIT_TYPE; -- 16 + Word_6 : A_16_BIT_TYPE; -- 16 = end record; -- 160 bits for SAMPLE_REC_TYPE'size use 160; -- Guarantees known size --------------------------------------------- -- Declare array type of appropiate size to - -- map to above record definition. - --------------------------------------------- Sample_Rec_Words : constant := SAMPLE_REC_TYPE'size / 8; type BYTE_ARRAY_TYPE is array (1 .. Sample_Rec_Words) of BYTE; pragma Pack(BYTE_ARRAY_TYPE); type INT_ARRAY_PTR is access BYTE_ARRAY_TYPE; --------------------------------------------- -- Declare Unchecked_Conversion function to - -- permit assigning the address of X to Y. - -- Y := X'address; - --------------------------------------------- function To_Ptr is new Unchecked_Conversion(ADDRESS, INT_ARRAY_PTR); ------------------------------------ -- Declare object of record type - -- and access object of array type - ------------------------------------ X : SAMPLE_REC_TYPE; Y : INT_ARRAY_PTR := To_Ptr(X'address); -- Don't use 'new' I have used this method on several occasions and it works just fine. Howeve r, it does seem a little complicated and makes the assumption that an object of an access type is implemented using the address of the object. Method 2: Just use a representation clause to map one of the objects to the address of the other. X : SAMPLE_REC_TYPE; Y : BYTE_ARRAY_TYPE; for Y use at X'address; This seems to be much more straight forward and makes no assumptions about the implementation. But when this method is used a compiler (Telesoft) warning is issued to the affect that the representation clause should not be used to produce overlayed data? Question: Which of the above methods is prefered? Is there a better way? Bob