From: Marin David Condic <condicma@bogon.pwfl.com>
Subject: Re: Unbounded string deallocation
Date: 1999/08/26
Date: 1999-08-26T00:00:00+00:00 [thread overview]
Message-ID: <37C56E59.218CA190@pwfl.com> (raw)
In-Reply-To: 37c552fd@news1.us.ibm.net
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/
next prev parent reply other threads:[~1999-08-26 0:00 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` David C. Hoos, Sr.
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 ` Marin David Condic [this message]
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 ` Michael F. Yoder
1999-08-26 0:00 ` Pascal Obry
1999-08-26 0:00 ` David C. Hoos, Sr.
1999-08-30 0:00 ` Andy Askey
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox