comp.lang.ada
 help / color / mirror / Atom feed
* Re: Unbounded string deallocation
  1999-08-25  0:00 Unbounded string deallocation Andy Askey
@ 1999-08-25  0:00 ` Martin C. Carlisle
  1999-08-26  0:00   ` Andy Askey
  1999-08-26  0:00   ` Pascal Obry
  1999-08-26  0:00 ` David C. Hoos, Sr.
  1999-08-30  0:00 ` Andy Askey
  2 siblings, 2 replies; 14+ messages in thread
From: Martin C. Carlisle @ 1999-08-25  0:00 UTC (permalink / raw)


Actually, the great thing about Ada.Strings.Unbounded is that the
LRM requires it not to leak memory.  See LRM A.4.5(88).
This means that when an unbounded string is reassigned, or goes out of 
scope, the memory will be reclaimed.

To use it for a brief period of time, use a declare block.

--Martin

In article <37C46FD4.A42CC1A1@res.raytheon.com>,
Andy Askey  <askeya@res.raytheon.com> wrote:
>I am looking into using Ada.Strings.Unbounded for dynamically creating
>temporary storage of character arrays.  When I am finished processing
>the string, I want to deallocate it.  I found a "Free" routine in the
>spec but this uses a pointer to STRING and not UNBOUNDED_STRING.
>
>From the Apex ada.strings.unbounded.1.ada =>
>
> type Unbounded_String is private;
> type String_Access is access all String;
> procedure Free (X : in out String_Access);
>
>Can anyone help me out with the deallocation?  Anyone have a better idea
>for dynamically allocating and deallocating memory for a small to medium
>size byte array?
>
>Thanx.
>-- 
>---------------------------------------------------
>|                 Andy Askey                      |
>|              Software Engineer                  |
>|           Raytheon Systems Company              |
>|   670 Discovery Drive, Huntsville, AL  35806    |
>|   Phone: (256) 971-2367  Fax: (256) 971-2306    |
>|        andrew_j_askey@res.raytheon.com          |
>---------------------------------------------------


-- 
Martin C. Carlisle, Asst Prof of Computer Science, US Air Force Academy
carlislem@acm.org, http://www.usafa.af.mil/dfcs/bios/carlisle.html
DISCLAIMER:  This content in no way reflects the opinions, standards or 
policy of the US Air Force Academy or the United States Government.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Unbounded string deallocation
@ 1999-08-25  0:00 Andy Askey
  1999-08-25  0:00 ` Martin C. Carlisle
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Andy Askey @ 1999-08-25  0:00 UTC (permalink / raw)


I am looking into using Ada.Strings.Unbounded for dynamically creating
temporary storage of character arrays.  When I am finished processing
the string, I want to deallocate it.  I found a "Free" routine in the
spec but this uses a pointer to STRING and not UNBOUNDED_STRING.

From the Apex ada.strings.unbounded.1.ada =>

 type Unbounded_String is private;
 type String_Access is access all String;
 procedure Free (X : in out String_Access);

Can anyone help me out with the deallocation?  Anyone have a better idea
for dynamically allocating and deallocating memory for a small to medium
size byte array?

Thanx.
-- 
---------------------------------------------------
|                 Andy Askey                      |
|              Software Engineer                  |
|           Raytheon Systems Company              |
|   670 Discovery Drive, Huntsville, AL  35806    |
|   Phone: (256) 971-2367  Fax: (256) 971-2306    |
|        andrew_j_askey@res.raytheon.com          |
---------------------------------------------------




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
       [not found]     ` <37c552fd@news1.us.ibm.net>
  1999-08-26  0:00       ` Michael F. Yoder
@ 1999-08-26  0:00       ` Marin David Condic
  1999-08-29  0:00         ` Robert Dewar
  1 sibling, 1 reply; 14+ messages in thread
From: Marin David Condic @ 1999-08-26  0:00 UTC (permalink / raw)


Matthew Heaney wrote:

> >   -- grab approx 1024 bytes of memory
> >   my_string := new ada.strings.unbounded_string.To_Unbound_String(1024);
> >
> >   -- give back the 1024 bytes and grab 2048 new memory bytes
> >   my_string := new ada.strings.unbounded_string.To_Unbound_String(2048);
> >
> > end
> >
> > Is this correct?
>
> No, it is completely incorrect:
>
> o  Do NOT use the allocator new.  Any allocation is done implicitly, as part
> of the call to To_Unbounded_String.
>
> o  To_Unbounded_String takes type String as an argument; it does NOT take an
> integer type.
>

In fairness, the package Ada.Strings.Unbounded does provide a To_Unbounded_String
which will take a length as a parameter. It also provides an access type for the
Fixed Strings. The example above is incorrect because it is mixing String_Access
types with Unbounded_String types, but with a little adjustment, one could have
done the dynamic allocation and used the Free procedure for deallocation - although
I can't for the life of me think of why anyone would want to if nobody was standing
over them with a stick forcing them to.

IMHO, the To_Unbounded_String and To_String are just about all you need for most
applications. And if Text_IO and some other basic things had Unbounded_String
alternatives, you wouldn't even need these calls. I suppose there might be
applications with efficiency issues, but for most things Unbounded_String is quite
adequate.

>
> Here's what you do:
>
>   declare
>     My_String : Unbounded_String;
>   begin
>     My_String := To_Unbounded_String ("this is a string");
>     -- all allocation is implicit
>
>     My_String := To_Unbounded_String ("this is another string");
>     -- all deallocation is implicit
>     -- all allocation is implicit
>
>     My_String := To_Unbounded_String ("this is yet another string");
>     -- all deallocation is implicit
>     -- all allocation is implicit
>   end;
>   -- all deallocation is implicit

A very clear example. I think that C programmers coming over to Ada have a tendency
to want to grab pointers to everything or null-terminate strings unnecessarily just
because that is what they are used to doing. Eventually, they get used to "The Ada
Way". :-)

MDC
--
Marin David Condic
Real Time & Embedded Systems, Propulsion Systems Analysis
United Technologies, Pratt & Whitney, Large Military Engines
M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600
***To reply, remove "bogon" from the domain name.***

Visit my web page at: http://www.mcondic.com/






^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-25  0:00 Unbounded string deallocation Andy Askey
  1999-08-25  0:00 ` Martin C. Carlisle
@ 1999-08-26  0:00 ` David C. Hoos, Sr.
  1999-08-30  0:00 ` Andy Askey
  2 siblings, 0 replies; 14+ messages in thread
From: David C. Hoos, Sr. @ 1999-08-26  0:00 UTC (permalink / raw)



Andy Askey <askeya@res.raytheon.com> wrote in message
news:37C46FD4.A42CC1A1@res.raytheon.com...
> I am looking into using Ada.Strings.Unbounded for dynamically creating
> temporary storage of character arrays.  When I am finished processing
> the string, I want to deallocate it.  I found a "Free" routine in the
> spec but this uses a pointer to STRING and not UNBOUNDED_STRING.
>
> From the Apex ada.strings.unbounded.1.ada =>
>
>  type Unbounded_String is private;
>  type String_Access is access all String;
>  procedure Free (X : in out String_Access);
>
> Can anyone help me out with the deallocation?  Anyone have a better idea
> for dynamically allocating and deallocating memory for a small to medium
> size byte array?
>
Not only does the LRM guarantee that unbounded strings are automatically
deallocated when they go out of scope (as Martin Carlisle has pointed out),
the implementation of Unbounded strings (at least in the GNAT compiler)
provides a good example of how to provide this kind of behavior for
any type.  The use of the Ada95 controlled type mechanism makes memory
management much more straightforward.

Section 7.6 of the LRM discusses controlled types in detail.







^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-26  0:00   ` Andy Askey
  1999-08-26  0:00     ` Martin C. Carlisle
       [not found]     ` <37c552fd@news1.us.ibm.net>
@ 1999-08-26  0:00     ` David C. Hoos, Sr.
  1999-08-26  0:00     ` Andy Askey
  3 siblings, 0 replies; 14+ messages in thread
From: David C. Hoos, Sr. @ 1999-08-26  0:00 UTC (permalink / raw)



Andy Askey <askeya@res.raytheon.com> wrote in message
news:37C54509.17E067C1@res.raytheon.com...
> Martin,
> Thanx for the info.  My unbound string never goes out of scope until I
> terminate my program (which is why I "thought" needed to deallocate it
> myself).  If all I have to do to reclaim the memory is to create a new
> string using the same variable, then this takes care of all my
> problems.  Just to make sure I understand this correctly:
>
> my_string : ada.strings.unbounded_string;
> begin
>
>   -- grab approx 1024 bytes of memory
>   my_string := new ada.strings.unbounded_string.To_Unbound_String(1024);
>
>   -- give back the 1024 bytes and grab 2048 new memory bytes
>   my_string := new ada.strings.unbounded_string.To_Unbound_String(2048);
>
> end
>
> Is this correct.  I know am being simplistic with the actually memory
> bytes allocated. But do I have the theory correct?
>

It's even simpler.  One doesn't need to allocate any particular size.
One can just keep appending, as necessary.  See the following example.
After use, assigning the null_unbounded_string deallocates the memory.
Then, when desired, begin appending again.

with Ada.Text_IO;
with Ada.Strings.Unbounded;
procedure Demo_Unbounded_Strings is
   My_String : Ada.Strings.Unbounded.Unbounded_String;
begin
   Ada.Strings.Unbounded.append
     (Source   => My_String,
      New_Item => "Now is the time");
   Ada.Strings.Unbounded.append
     (Source   => My_String,
      New_Item => " for all good men");
   Ada.Strings.Unbounded.append
     (Source   => My_String,
      New_Item => " to come to the aid of their country.");

   Ada.Text_Io.Put_Line
     (Ada.Strings.Unbounded.To_String (My_String));

   My_String := Ada.Strings.Unbounded.Null_Unbounded_String;

end Demo_Unbounded_Strings;
---------------------------------------------------






^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
       [not found]     ` <37c552fd@news1.us.ibm.net>
@ 1999-08-26  0:00       ` Michael F. Yoder
  1999-08-26  0:00       ` Marin David Condic
  1 sibling, 0 replies; 14+ messages in thread
From: Michael F. Yoder @ 1999-08-26  0:00 UTC (permalink / raw)


Matthew Heaney wrote:

> o  To_Unbounded_String takes type String as an argument; it does NOT take an
> integer type.

Not so, Matthew.  See A.4.5(10) and A.4.5(76).  The "new" in Andy's code
is incorrect, and the function should be named To_Unbounded_String, but
this is a way to yield an uninitialized string of the desired length.

-- 
Michael F. Yoder

Unscientific man is beset by a deplorable desire to have been right.
The scientist is distinguished by a desire to *be* right. -- W.V. Quine




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-26  0:00   ` Andy Askey
@ 1999-08-26  0:00     ` Martin C. Carlisle
       [not found]     ` <37c552fd@news1.us.ibm.net>
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Martin C. Carlisle @ 1999-08-26  0:00 UTC (permalink / raw)


As Pascal pointed out, you are absolutely correct.  A reassignment will
reclaim the previous storage.

A common implementation of Unbounded_Strings is to use Controlled types.
Using controlled types you can create a lot of such data structures that
manage their own memory.

--Martin

In article <37C54509.17E067C1@res.raytheon.com>,
Andy Askey  <askeya@res.raytheon.com> wrote:
>Martin,
>Thanx for the info.  My unbound string never goes out of scope until I
>terminate my program (which is why I "thought" needed to deallocate it
>myself).  If all I have to do to reclaim the memory is to create a new
>string using the same variable, then this takes care of all my
>problems.  Just to make sure I understand this correctly:
>
>my_string : ada.strings.unbounded_string;
>begin
>
>  -- grab approx 1024 bytes of memory
>  my_string := new ada.strings.unbounded_string.To_Unbound_String(1024);
>
>  -- give back the 1024 bytes and grab 2048 new memory bytes
>  my_string := new ada.strings.unbounded_string.To_Unbound_String(2048);
>
>end
-- 
Martin C. Carlisle, Asst Prof of Computer Science, US Air Force Academy
carlislem@acm.org, http://www.usafa.af.mil/dfcs/bios/carlisle.html
DISCLAIMER:  This content in no way reflects the opinions, standards or 
policy of the US Air Force Academy or the United States Government.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-25  0:00 ` Martin C. Carlisle
@ 1999-08-26  0:00   ` Andy Askey
  1999-08-26  0:00     ` Martin C. Carlisle
                       ` (3 more replies)
  1999-08-26  0:00   ` Pascal Obry
  1 sibling, 4 replies; 14+ messages in thread
From: Andy Askey @ 1999-08-26  0:00 UTC (permalink / raw)


Martin,
Thanx for the info.  My unbound string never goes out of scope until I
terminate my program (which is why I "thought" needed to deallocate it
myself).  If all I have to do to reclaim the memory is to create a new
string using the same variable, then this takes care of all my
problems.  Just to make sure I understand this correctly:

my_string : ada.strings.unbounded_string;
begin

  -- grab approx 1024 bytes of memory
  my_string := new ada.strings.unbounded_string.To_Unbound_String(1024);

  -- give back the 1024 bytes and grab 2048 new memory bytes
  my_string := new ada.strings.unbounded_string.To_Unbound_String(2048);

end

Is this correct.  I know am being simplistic with the actually memory
bytes allocated. But do I have the theory correct?

Thanx.
Andy


"Martin C. Carlisle" wrote:
> 
> Actually, the great thing about Ada.Strings.Unbounded is that the
> LRM requires it not to leak memory.  See LRM A.4.5(88).
> This means that when an unbounded string is reassigned, or goes out of
> scope, the memory will be reclaimed.
> 
> To use it for a brief period of time, use a declare block.
> 
> --Martin
> 
> In article <37C46FD4.A42CC1A1@res.raytheon.com>,
> Andy Askey  <askeya@res.raytheon.com> wrote:
> >I am looking into using Ada.Strings.Unbounded for dynamically creating
> >temporary storage of character arrays.  When I am finished processing
> >the string, I want to deallocate it.  I found a "Free" routine in the
> >spec but this uses a pointer to STRING and not UNBOUNDED_STRING.
> >
> >From the Apex ada.strings.unbounded.1.ada =>
> >
> > type Unbounded_String is private;
> > type String_Access is access all String;
> > procedure Free (X : in out String_Access);
> >
> >Can anyone help me out with the deallocation?  Anyone have a better idea
> >for dynamically allocating and deallocating memory for a small to medium
> >size byte array?
> >
> >Thanx.
> >--
> >---------------------------------------------------
> >|                 Andy Askey                      |
> >|              Software Engineer                  |
> >|           Raytheon Systems Company              |
> >|   670 Discovery Drive, Huntsville, AL  35806    |
> >|   Phone: (256) 971-2367  Fax: (256) 971-2306    |
> >|        andrew_j_askey@res.raytheon.com          |
> >---------------------------------------------------
> 
> --
> Martin C. Carlisle, Asst Prof of Computer Science, US Air Force Academy
> carlislem@acm.org, http://www.usafa.af.mil/dfcs/bios/carlisle.html
> DISCLAIMER:  This content in no way reflects the opinions, standards or
> policy of the US Air Force Academy or the United States Government.

-- 
---------------------------------------------------
|                 Andy Askey                      |
|              Software Engineer                  |
|           Raytheon Systems Company              |
|   670 Discovery Drive, Huntsville, AL  35806    |
|   Phone: (256) 971-2367  Fax: (256) 971-2306    |
|        andrew_j_askey@res.raytheon.com          |
---------------------------------------------------




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-26  0:00   ` Andy Askey
                       ` (2 preceding siblings ...)
  1999-08-26  0:00     ` David C. Hoos, Sr.
@ 1999-08-26  0:00     ` Andy Askey
  3 siblings, 0 replies; 14+ messages in thread
From: Andy Askey @ 1999-08-26  0:00 UTC (permalink / raw)


Please overlook the syntax errors.

Thanx.
Andy




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-25  0:00 ` Martin C. Carlisle
  1999-08-26  0:00   ` Andy Askey
@ 1999-08-26  0:00   ` Pascal Obry
  1 sibling, 0 replies; 14+ messages in thread
From: Pascal Obry @ 1999-08-26  0:00 UTC (permalink / raw)



Just to add that if you want to "release" the memory used by a very 
large Unbounded_String without leaving the declaration scope, you 
can assigned to it the value Null_Unbounded_String declared
in Ada.Strings.Unbounded.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://ourworld.compuserve.com/homepages/pascal_obry
--|
--| "The best way to travel is by means of imagination"





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-26  0:00       ` Marin David Condic
@ 1999-08-29  0:00         ` Robert Dewar
  1999-08-30  0:00           ` Marin David Condic
  0 siblings, 1 reply; 14+ messages in thread
From: Robert Dewar @ 1999-08-29  0:00 UTC (permalink / raw)


In article <37C56E59.218CA190@pwfl.com>,
  e108678@pwflcom wrote:
 And if Text_IO and some other basic things had Unbounded_String
> alternatives, you wouldn't even need these calls.

Note the package Ada.Strings.Unbounded.Text_IO
in GNAT!

Robert Dewar
Ada Core Technologies


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-29  0:00         ` Robert Dewar
@ 1999-08-30  0:00           ` Marin David Condic
  1999-08-30  0:00             ` Robert Dewar
  0 siblings, 1 reply; 14+ messages in thread
From: Marin David Condic @ 1999-08-30  0:00 UTC (permalink / raw)


Robert Dewar wrote:

> In article <37C56E59.218CA190@pwfl.com>,
>   e108678@pwflcom wrote:
>  And if Text_IO and some other basic things had Unbounded_String
> > alternatives, you wouldn't even need these calls.
>
> Note the package Ada.Strings.Unbounded.Text_IO
> in GNAT!

Very nice. However it is clearly not a part of the language standard, so
if you need portability for some reason, this might be a problem. It
seems like something that ought to be included in a future version of
Ada since for a large number of plain vanilla apps, unbounded strings
are far more convenient where speed is not an issue. (For example,
something that implements a command line interpreter as its only real
use of strings)

MDC
--
Marin David Condic
Real Time & Embedded Systems, Propulsion Systems Analysis
United Technologies, Pratt & Whitney, Large Military Engines
M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600
***To reply, remove "bogon" from the domain name.***

Visit my web page at: http://www.mcondic.com/






^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-30  0:00           ` Marin David Condic
@ 1999-08-30  0:00             ` Robert Dewar
  0 siblings, 0 replies; 14+ messages in thread
From: Robert Dewar @ 1999-08-30  0:00 UTC (permalink / raw)


In article <37CA8ABF.21F6F177@pwfl.com>,
  e108678@pwflcom wrote:
> Robert Dewar wrote:
>
> > In article <37C56E59.218CA190@pwfl.com>,
> >   e108678@pwflcom wrote:
> >  And if Text_IO and some other basic things had
Unbounded_String
> > > alternatives, you wouldn't even need these calls.
> >
> > Note the package Ada.Strings.Unbounded.Text_IO
> > in GNAT!
>
> Very nice. However it is clearly not a part of the language
standard, so
> if you need portability for some reason, this might be a
problem.

It is of course provided in all versions of GNAT, so it is
fully portable across all GNAT platforms. Generally when we
make something a grandchild in Ada, rather than making it
a child of GNAT, we are suggesting that we think it is a
reasonable addition for other vendors to copy (this is much
more relevant than worrying about the next version of the
standard which, if it appears, is many years away).

Robert Dewar
Ada Core Technologies


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unbounded string deallocation
  1999-08-25  0:00 Unbounded string deallocation Andy Askey
  1999-08-25  0:00 ` Martin C. Carlisle
  1999-08-26  0:00 ` David C. Hoos, Sr.
@ 1999-08-30  0:00 ` Andy Askey
  2 siblings, 0 replies; 14+ messages in thread
From: Andy Askey @ 1999-08-30  0:00 UTC (permalink / raw)


Thanx to all who provided info.  It appears the unbound strings
allocation/deallocation will significantly reduce the amount of code I
will have to write for my application.

Andy

Andy Askey wrote:
> 
> I am looking into using Ada.Strings.Unbounded for dynamically creating
> temporary storage of character arrays.  When I am finished processing
> the string, I want to deallocate it.  I found a "Free" routine in the
> spec but this uses a pointer to STRING and not UNBOUNDED_STRING.
> 
> From the Apex ada.strings.unbounded.1.ada =>
> 
>  type Unbounded_String is private;
>  type String_Access is access all String;
>  procedure Free (X : in out String_Access);
> 
> Can anyone help me out with the deallocation?  Anyone have a better idea
> for dynamically allocating and deallocating memory for a small to medium
> size byte array?
> 
> Thanx.
> --
> ---------------------------------------------------
> |                 Andy Askey                      |
> |              Software Engineer                  |
> |           Raytheon Systems Company              |
> |   670 Discovery Drive, Huntsville, AL  35806    |
> |   Phone: (256) 971-2367  Fax: (256) 971-2306    |
> |        andrew_j_askey@res.raytheon.com          |
> ---------------------------------------------------

-- 
---------------------------------------------------
|                 Andy Askey                      |
|              Software Engineer                  |
|           Raytheon Systems Company              |
|   670 Discovery Drive, Huntsville, AL  35806    |
|   Phone: (256) 971-2367  Fax: (256) 971-2306    |
|        andrew_j_askey@res.raytheon.com          |
---------------------------------------------------




^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~1999-08-30  0:00 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-25  0:00 Unbounded string deallocation Andy Askey
1999-08-25  0:00 ` Martin C. Carlisle
1999-08-26  0:00   ` Andy Askey
1999-08-26  0:00     ` Martin C. Carlisle
     [not found]     ` <37c552fd@news1.us.ibm.net>
1999-08-26  0:00       ` Michael F. Yoder
1999-08-26  0:00       ` Marin David Condic
1999-08-29  0:00         ` Robert Dewar
1999-08-30  0:00           ` Marin David Condic
1999-08-30  0:00             ` Robert Dewar
1999-08-26  0:00     ` David C. Hoos, Sr.
1999-08-26  0:00     ` Andy Askey
1999-08-26  0:00   ` Pascal Obry
1999-08-26  0:00 ` David C. Hoos, Sr.
1999-08-30  0:00 ` Andy Askey

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