Recent

Author Topic: How to dispose an array pointer.  (Read 14332 times)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to dispose an array pointer.
« Reply #15 on: October 15, 2013, 10:51:25 am »
Quote
If I declare a dynamic array like Astr: array of string[100], then I need to use SetLength while I don't know how many Length yet, so it do not meet my need. And if I use Tstringlist it's good for String, but not for Longint or other type.
Do you know why generics is invented? To cover case like this:
Code: [Select]
uses
  fgl;
...
type
  TLongIntList = specialize TFPGList<LongInt>;
  TStringList = specialize TFPGList<String>;
...
var
  LL: TLongIntList;
  SL: TStringList;
  i: Integer;
begin
  LL := TLongIntList.Create;
  SL := TStringList.Create;
  try
    LL.Add(1);
    LL.Add(2);
    LL.Add(3);
    LL.Add(4);
    SL.Add('this');
    SL.Add('is');
    SL.Add('my');
    SL.Add('list');
    for i := 0 to LL.Count - 1 begin
      WriteLn(LL[i]);
    end;
    for i := 0 to SL.Count - 1 begin
      WriteLn(SL[i]);
    end;
end;
Quote
If I use similar codes above, I can create a dynamic array (without use SetLength) of any type.
What do you think?
As I've explained, it's wrong. You're overwriting some other memory region. New() allocates only as much as the pointed type.
« Last Edit: October 15, 2013, 10:53:07 am by Leledumbo »

 

TinyPortal © 2005-2018