comp.lang.ada
 help / color / mirror / Atom feed
* Array of unbounded strings, memory leakage?
@ 2002-10-20 16:20 Eric G. Miller
  2002-10-20 19:08 ` Jeffrey Carter
  2002-10-22 14:36 ` Matthew Heaney
  0 siblings, 2 replies; 4+ messages in thread
From: Eric G. Miller @ 2002-10-20 16:20 UTC (permalink / raw)


I know the following isn't terribly efficient, but does the Resize
procedure leak memory?  I wrote a little package for reading/writing
common delimited text file formats (tab, space, comma, etc...) and 
this is what I'm using to hold the split contents of each "record".
If I understand correctly, it shouldn't leak, since I never "new" the
individual unbounded strings.

Say I have:

  type UBString_Array is array (Positive range <>) of
       Ada.Strings.Unbounded.Unbounded_String;
  type UBStrArray_Access is access UBString_Array;

...

  procedure Free is new Ada.Unchecked_Deallocation (
        UBString_Array, UBStrArray_Access);

  procedure Resize (Fields : in out UBStrArray_Access;
                    Size   : Natural)
  is
      Tmp : UBStrArray_Access := null;
      Old_Size : Natural := 0;
  begin
      if Fields /= null then
          Old_Size := Fields'Length;
      end if;
      if Size > 0 then
          Tmp := new UBString_Array(1..Size);
          if Size <= Old_Size then
              Tmp(1..Size) := Fields(1..Size);
          else
              Tmp(1..Old_Size) := Fields(1..Old_Size);
              for J in Old_Size + 1 .. Size loop
                  Tmp(J) := Ada.Strings.Unbounded.Null_Unbounded_String;
              end loop;
          end if;
      end if;
      Free (Fields);
      Fields := Tmp;
  end Resize;




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

* Re: Array of unbounded strings, memory leakage?
  2002-10-20 16:20 Array of unbounded strings, memory leakage? Eric G. Miller
@ 2002-10-20 19:08 ` Jeffrey Carter
  2002-10-20 20:16   ` Eric G. Miller
  2002-10-22 14:36 ` Matthew Heaney
  1 sibling, 1 reply; 4+ messages in thread
From: Jeffrey Carter @ 2002-10-20 19:08 UTC (permalink / raw)


Eric G. Miller wrote:
> I know the following isn't terribly efficient, but does the Resize
> procedure leak memory?  I wrote a little package for reading/writing
> common delimited text file formats (tab, space, comma, etc...) and 
> this is what I'm using to hold the split contents of each "record".
> If I understand correctly, it shouldn't leak, since I never "new" the
> individual unbounded strings.
> 
> Say I have:
> 
>   type UBString_Array is array (Positive range <>) of
>        Ada.Strings.Unbounded.Unbounded_String;
>   type UBStrArray_Access is access UBString_Array;
> 
> ...
> 
>   procedure Free is new Ada.Unchecked_Deallocation (
>         UBString_Array, UBStrArray_Access);
> 
>   procedure Resize (Fields : in out UBStrArray_Access;
>                     Size   : Natural)
>   is
>       Tmp : UBStrArray_Access := null;
>       Old_Size : Natural := 0;
>   begin
>       if Fields /= null then
>           Old_Size := Fields'Length;
>       end if;
>       if Size > 0 then
>           Tmp := new UBString_Array(1..Size);
>           if Size <= Old_Size then
>               Tmp(1..Size) := Fields(1..Size);
>           else
>               Tmp(1..Old_Size) := Fields(1..Old_Size);
>               for J in Old_Size + 1 .. Size loop
>                   Tmp(J) := Ada.Strings.Unbounded.Null_Unbounded_String;
>               end loop;
>           end if;
>       end if;
>       Free (Fields);
>       Fields := Tmp;
>   end Resize;
> 

This shouldn't leak memory. If it does, there's a problem with your 
compiler's implementation of Unbounded_String.

Note that the loop to initialize the new elements could use an aggregate:

Tmp (Old_Size + 1 .. Size) := (others => Null_Unbounded_String);

However, since an uninitialized Unbounded_String has the default value 
of Null_Unbounded_String, this isn't necessary.

-- 
Jeff Carter
"Sons of a silly person."
Monty Python & the Holy Grail




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

* Re: Array of unbounded strings, memory leakage?
  2002-10-20 19:08 ` Jeffrey Carter
@ 2002-10-20 20:16   ` Eric G. Miller
  0 siblings, 0 replies; 4+ messages in thread
From: Eric G. Miller @ 2002-10-20 20:16 UTC (permalink / raw)


In <3DB2FF06.8060308@acm.org>, Jeffrey Carter wrote:

> This shouldn't leak memory. If it does, there's a problem with your 
> compiler's implementation of Unbounded_String.

Thanks for the info.  It's what I thought, but wasn't sure...
 
> Note that the loop to initialize the new elements could use an aggregate:
> 
> Tmp (Old_Size + 1 .. Size) := (others => Null_Unbounded_String);

Doh!
 
> However, since an uninitialized Unbounded_String has the default value 
> of Null_Unbounded_String, this isn't necessary.

Also wasn't sure of that either (haven't used Unbounded Strings much)...



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

* Re: Array of unbounded strings, memory leakage?
  2002-10-20 16:20 Array of unbounded strings, memory leakage? Eric G. Miller
  2002-10-20 19:08 ` Jeffrey Carter
@ 2002-10-22 14:36 ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 2002-10-22 14:36 UTC (permalink / raw)



"Eric G. Miller" <egm2@jps-nospam.net> wrote in message
news:pan.2002.10.20.16.23.48.564315@jps-nospam.net...
> I know the following isn't terribly efficient, but does the Resize
> procedure leak memory?

Instances of Unbounded_String --even if they are components of an array--
don't leak memory.  Typically they would be implemented as a private
derivation from Controlled.

Of course, if you allocate the array on the heap, and then forget to
deallocate the array, then of course that is a memory leak.

Programmers sometimes forget (or perhaps don't even realize) that
Unchecked_Deallocation is "aware" that the object being deallocating is
Controlled, and Finalization will occur before the actual memory
reclaimation happens.  This is not unlike how delete works in C++: the dtor
will get invoked prior to actual deallocation.

The Charles library already has an unbounded vector you could use, instead
of manipulating unbounded arrays directly.  For example:

package Unbounded_String_Vectors is
   new Charles.Vectors.Unbounded (Unbounded_String);
use Unbounded_String_Vectors;

V : Container_Type;
...
Resize (V, Size => N);
--or--
Assign (V, Length => N);
--or--
Assign (V, Length => N, Item => Null_Unbounded_String);

The Charles vector differentiates between "length" and "size".  It wasn't
clear from your example whether the length or size needed modification, so
the fragment above shows you how to do both.

http://home.earthlink.net/~matthewjheaney/charles/charles-vectors-unbounded.
html

I just released a new version (20021021) of the library, with better error
checking.

http://home.earthlink.net/~matthewjheaney/charles/index.html







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

end of thread, other threads:[~2002-10-22 14:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-20 16:20 Array of unbounded strings, memory leakage? Eric G. Miller
2002-10-20 19:08 ` Jeffrey Carter
2002-10-20 20:16   ` Eric G. Miller
2002-10-22 14:36 ` Matthew Heaney

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