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

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