comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Ada and string literals
Date: Wed, 30 Jan 2013 09:19:17 -0800 (PST)
Date: 2013-01-30T09:19:17-08:00	[thread overview]
Message-ID: <adee1317-64e7-4a2e-9d99-324b55618d14@googlegroups.com> (raw)
In-Reply-To: <ea721d9c-7f42-4908-9787-ef8b8242428a@googlegroups.com>

On Wednesday, January 30, 2013 8:52:32 AM UTC-8, codeallergy wrote:
> Okay. assuming dat we use GNAT 2012
> 
> Literal1 : aliased constant String := "LITERAL"; 
> Literal2 : aliased constant String := "LITERAL"; 
> Literal3 : aliased constant String := "LITERAL"; 
> 
> how the above code is handled ?
> 
> 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 share the same memory causes a problem if the program later compares the 'Access 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 memory.  (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 ?
> 
> example:
> 
> procedure Free_Abc (Target : access String)
> is
> begin
>    GNAT.Strings.Free(Target); -- error
> end Free_Abc;
> 
> this code produce the error: actual for "X" must be a variable
> 
> why disallow that ?

Because the definition of Free is that you give it an access variable, it frees 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 get around it easily enough by declaring a Temp variable and then

   Temp := 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



  reply	other threads:[~2013-01-30 17:19 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-30  0:44 Ada and string literals codeallergy
2013-01-30  7:08 ` Niklas Holsti
2013-01-30 11:50   ` Mart van de Wege
2013-01-30 13:52     ` Niklas Holsti
2013-01-30 16:09       ` Adam Beneschan
2013-02-01  0:54       ` Shark8
2013-02-01  9:03         ` Niklas Holsti
2013-02-01 14:58           ` Shark8
2013-01-30 16:52     ` codeallergy
2013-01-30 17:19       ` Adam Beneschan [this message]
2013-01-30 20:02       ` Simon Wright
2013-01-30 20:19         ` Georg Bauhaus
2013-01-30 22:06       ` Robert A Duff
2013-01-30 22:10         ` Jeffrey Carter
2013-01-31  2:23           ` Robert A Duff
2013-01-31 15:49             ` Adam Beneschan
2013-01-31 22:24               ` Robert A Duff
2013-02-01 21:16       ` gautier_niouzes
2013-02-02  1:55       ` Stephen Leake
2013-02-02 14:30         ` Robert A Duff
2013-01-31  9:20     ` ake.ragnar.dahlgren
2013-01-30 16:20   ` Robert A Duff
2013-02-01 13:20     ` Stephen Leake
2013-02-01 14:49       ` Robert A Duff
2013-02-01 17:23       ` Dmitry A. Kazakov
2013-02-01 20:22         ` Robert A Duff
2013-02-01 22:03           ` Dmitry A. Kazakov
2013-01-30 22:54 ` codeallergy
2013-02-01  0:50 ` Shark8
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox