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,7be1cfc685fd1ce2 X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Access types vs. Record types as procedure parameters Date: 1999/08/19 Message-ID: <37bc69fe@news1.us.ibm.net>#1/1 X-Deja-AN: 514825338 Content-transfer-encoding: 7bit References: <37BC4F12.E33D50EE@gbr.msd.ray.com> X-Trace: 19 Aug 1999 20:33:02 GMT, 129.37.213.244 Organization: Global Network Services - Remote Access Mail & News Services X-Notice: should be reported to postmaster@ibm.net Content-Type: text/plain; charset="US-ASCII" Mime-version: 1.0 Newsgroups: comp.lang.ada X-Complaints-To: postmaster@ibm.net Date: 1999-08-19T00:00:00+00:00 List-Id: In article <37BC4F12.E33D50EE@gbr.msd.ray.com> , Andrew L Moore {66003} wrote: > I am debating on whether to pass a record object to a procedure or an > access type to that record object. > > For example I have: > > package body X is > type Record_Type; > type Array_Type is array (1 .. n) of Record_Type; > Array_Object : Array_Type; > procedure One; > procedure Two (Parameter : in Record_Type); > ---More procedures and functions > end X; > procedure One is > begin > for I in range 1 .. n loop > procedure Two(Parameter => Array_Object (I)); > end loop; > end One; Do it this way, because the record type is *probably* being passed by reference anyway. If you want to guarantee that the record object is passed by reference, then declare it as tagged or limited: type RT is tagged record .. end record; type RT is limited record .. end record; > OR: > package body X is > type Record_Type; > type Array_Type is array(1 .. n) of Record_Type; > type Access_Type is access Record_Type; > Array_Object : Array_Type; > function Address_To_Pointer is new > Ada.Unchecked_Conversion(System.Address, Access_Type); > procedure One; > procedure Two (Parameter : in Access_Type); > --More procedures and functions > end X; > procedure One is > Object_Ptr : Access_Type := null; > begin > for I in range 1 .. n loop > > Object_Ptr := Address_To_Pointer(Array_Object(I)'Address) > procedure Two (Parameter => Object_Ptr); > end loop; > end One; This is a total hack. Do NOT do it this way. The language provides clean mechanisms for guaranteeing that your record is passed by reference, as I mentioned above. Another way closer in spirit to your 2nd method is to pass the record as an access parameter: package body X is type Record_Type; type Array_Type is array(1 .. n) of aliased Record_Type; ^^^^^^^ Array_Object : Array_Type; procedure One; procedure Two (Parameter : access Record_Type); ^^^^^^ --More procedures and functions end X; procedure One is begin for I in range 1 .. n loop procedure Two (Parameter => Array_Object (I)'Access); ^^^^^^ end loop; end One; Don't muck around with UC unless you know what you're doing, and you have a compelling need. You do NOT have a compelling need, so don't use UC. > I am looking for the way that would provide the greatest speed of > execution. I know that passing a pointer to an object of a record type > is more efficient that passing the record itself, but would that be > negated by repeated calls to Unchecked_Conversion. UC doesn't have any overhead. > Basically, are there > any advantages to using Unchecked_Conversion to get an access value to > an object to then pass it to a procedure. No, there are no advantages when you can declare a by-reference type (tagged and/or limited) or pass the object as an access parameter. -- Matt It is impossible to feel great confidence in a negative theory which has always rested its main support on the weak points of its opponent. Joseph Needham, "A Mechanistic Criticism of Vitalism"