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-Thread: 103376,dc1fff2721602dfa X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.66.84.167 with SMTP id a7mr563110paz.6.1359566357866; Wed, 30 Jan 2013 09:19:17 -0800 (PST) X-Received: by 10.50.5.174 with SMTP id t14mr823744igt.11.1359566357611; Wed, 30 Jan 2013 09:19:17 -0800 (PST) Path: 6ni25139pbd.1!nntp.google.com!f6no4566155pbd.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 30 Jan 2013 09:19:17 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ NNTP-Posting-Host: 66.126.103.122 References: <4978d638-a04b-4561-85e9-cf6620265af2@googlegroups.com> <86boc63n5d.fsf@gaheris.avalon.lan> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Ada and string literals From: Adam Beneschan Injection-Date: Wed, 30 Jan 2013 17:19:17 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2013-01-30T09:19:17-08:00 List-Id: On Wednesday, January 30, 2013 8:52:32 AM UTC-8, codeallergy wrote: > Okay. assuming dat we use GNAT 2012 >=20 > Literal1 : aliased constant String :=3D "LITERAL";=20 > Literal2 : aliased constant String :=3D "LITERAL";=20 > Literal3 : aliased constant String :=3D "LITERAL";=20 >=20 > how the above code is handled ? >=20 > compiler duplicate the string three times into the data section or > the three constants will share the same string or > one string into the data section but duplicated at the run time ? I don't know how GNAT handles it; but in Ada, having the string objects sha= re the same memory causes a problem if the program later compares the 'Acce= ss of two different objects. Ada says they're supposed to be unequal, but = I think that would be hard to do if the string literals shared the same mem= ory. (I can think of ways to do this by adding some additional data to the= access values, but that could easily result in way more overhead than just= duplicating the strings would take up.) > another question: how free a object from a procedure ? >=20 > example: >=20 > procedure Free_Abc (Target : access String) > is > begin > GNAT.Strings.Free(Target); -- error > end Free_Abc; >=20 > this code produce the error: actual for "X" must be a variable >=20 > why disallow that ? Because the definition of Free is that you give it an access variable, it f= rees the storage, then it sets the variable to null. Target is basically a= read-only value in this procedure, so you can't set it to null. You can g= et around it easily enough by declaring a Temp variable and then Temp :=3D Target; GNAT.Strings.Free(Temp); But then whoever called Free_Abc may still have a variable that points to a= string that no longer exists. So you have to be careful. I don't really = recommend having a procedure such as Free_Abc that frees storage and isn't = able to set the access to null; better to define a *named* access type that= is access-to-String and make it an IN OUT parameter to Free_Abc. -- Adam