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,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,74b55538385b7366,start X-Google-Attributes: gid103376,public From: "Vladimir Olensky" Subject: Which is right here - GNAT or OA ? Date: 1999/05/30 Message-ID: <928083159.436.79@news.remarQ.com>#1/1 X-Deja-AN: 483792160 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Complaints-To: newsabuse@remarQ.com X-Trace: 928083159.436.79 K3TLTKYJOA5C9C7F8C qube-02.us-ca.remarq.com Organization: Posted via RemarQ Communities, Inc. NNTP-Posting-Date: Sun, 30 May 1999 16:52:39 GMT Newsgroups: comp.lang.ada Date: 1999-05-30T00:00:00+00:00 List-Id: ---------------------------------- -- Which is right here - GNAT or OA ? --------------------------------- Recently I found some time to prepare for "AdaPower" some "touch and feel" examples (just brushing up my old exercises) on using controlled types for user defined GB which could be useful for people learning Ada. It is one of the ways to attract new people to Ada by explaining very simply some things that may seem very difficult. I mentioned about my simple user GB examples few months ago in GB thread but then I had no time to prepare them for open use. Then there was nice article about smart pointers by Mathew Heaney describing the same issue and I did not feel like adding anything else to this subject. Then AdaPower come to the existence (thanks to David Button) and I felt that it is better late than never. While working on that examples I also was preparing some introductory examples where I tried to put light on some things that may be unclear to the beginners. One of these intro examples is the effect of using Ada.Unchecked_Deallocation for pointers referencing different types of objects and objects allocated differently. (one may have a list of objects that are allocated differently and he/she wants that the same operations on the items of this list would have the same effect) To my surprise I found that GNAT 3.11p and OA (ObjectAda Special Edition version 7.1.424) give different results for the same test under Windows 98. Here is this example : ------------------------------------------------------ -- Author: Vladimir Olensky, May 1999 -- -- This example shows what happens when you are -- trying to deallocate static tagged object ( non controlled) --------------------------------------------------------- with Ada.Unchecked_Deallocation; with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Tst_A4 is Type Q is record ID:Integer:=0; end record; type Q_A is access all Q; procedure Free is new Ada.Unchecked_Deallocation(Q,Q_A); -- statically declared object q1: aliased Q; q_ptr:Q_A; begin q1.ID:=10; q_ptr:=q1'Access; Put(" Object ID = "); Put( (q_ptr.all.ID),2); New_Line; Put_line("-----------------------------------"); -- Statement_1: free(q_ptr); ------------------- q1.ID:=20; Put(" Object ID = "); -- Statement_2: Put( (q1.ID),2); ------------------- New_Line; Put(" q_ptr Object ID = "); -- Statement_3: Put( (q_ptr.all.ID),2); ---------------------------------- New_Line; end Tst_A4; ******************************************* In this test we have: GNAT: - executing statement_1 with no problems. after that we have: 1. q_ptr=null 2. q1 continue to exists As a result GNAT is executing Statement_2 with no problems and printing "Object ID = 20" and then raising CONSTRAINT_ERROR exception at Statement_3 (it should be so as q_ptr=null) OA: - raising exception at Statement_1: For me the right behavior of the Ada.Unchecked_Deallocation for pointer to statically allocated object (regardless of what RM is saying now) would be not to try to deallocate static object of any type - tagged or not tagged, controlled or uncontrolled and assign null value to pointer for which Ada.Unchecked_Deallocation was invoked. Why there is no such thing in RM Section 13.11.2 after paragraph 9 ? Were there any reasons not to describe this case? To me GNAT behavior is better than OA though is not consistent. ( for controlled types GNAT behaves differently - it raises PROGRAM_ERROR : EXCEPTION_ACCESS_VIOLATION). RM Section 13.11.2 does not describe clearly and explicitly this case so there is no wonder that the two best compiles behave differently. It is interesting if similar test is included in Ada compiles validation procedures. It would be interesting to hear comments regarding this issue. Regards, Vladimir_Olensky