comp.lang.ada
 help / color / mirror / Atom feed
From: "Eric G. Miller" <egm2@jps-nospam.net>
Subject: Array of unbounded strings, memory leakage?
Date: Sun, 20 Oct 2002 16:20:34 GMT
Date: 2002-10-20T16:20:34+00:00	[thread overview]
Message-ID: <pan.2002.10.20.16.23.48.564315@jps-nospam.net> (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;




             reply	other threads:[~2002-10-20 16:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-20 16:20 Eric G. Miller [this message]
2002-10-20 19:08 ` Array of unbounded strings, memory leakage? Jeffrey Carter
2002-10-20 20:16   ` Eric G. Miller
2002-10-22 14:36 ` Matthew Heaney
replies disabled

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