String Types 

www.madshi.net

Here is a list of "string" type definitions:

type
  {$ifndef d2009}
    UnicodeString   = WideString;
  {$endif}

  TPString          = ^string;
  TPAnsiString      = ^AnsiString;
  TPWideString      = ^WideString;
  TPUnicodeString   = ^UnicodeString;

  TAString          = array [0..maxInt shr 2-1] of string;
  TAAnsiString      = array [0..maxInt shr 2-1] of AnsiString;
  TAWideString      = array [0..maxInt shr 2-1] of WideString;
  TAUnicodeString   = array [0..maxInt shr 2-1] of UnicodeString;
  TPAString         = ^TAString;
  TPAAnsiString     = ^TAAnsiString;
  TPAWideString     = ^TAWideString;
  TPAUnicodeString  = ^TAUnicodeString;

  TDAString         = array of string;
  TDAAnsiString     = array of AnsiString;
  TDAWideString     = array of WideString;
  TDAUnicodeString  = array of UnicodeString;
  TPDAString        = ^TDAString;
  TPDAAnsiString    = ^TDAAnsiString;
  TPDAWideString    = ^TDAWideString;
  TPDAUnicodeString = ^TDAUnicodeString;

Please note, that allocating and freeing "TPString" or "TPAString" variables is a bit dangerous. You have to know exactly what you're doing. So please look at the following examples, which show wrong and right ways.

procedure Proc1;
var pStrVar : TPString;
begin
  // you can do this, but note, that the memory is not initialized when using "GetMem"
  GetMem(pStrVar, sizeOf(string));

  // "pStrVar^" is not initialized
  // -> Delphi thinks that "pStrVar^" references a valid string
  // -> Delphi tries to free it before assigning the new value
  // -> crash, because it is NOT a valid string, but a random value
  pStrVar^ := 'test';
end;

procedure Proc2;
var pStrVar : TPString;
begin
  // as you know already, "GetMem" does not initialize the memory
  GetMem(pStrVar, sizeOf(string));

  // so if you're doing it yourself, you're out of danger
  ZeroMemory(pStrVar, sizeOf(string));

  // "pStrVar^" is initialized this time
  // -> Delphi knows that "pStrVar^" holds no string currently
  // -> Delphi assigns the new value to "pStrVar^" successfully
  pStrVar^ := 'test';

  // FreeMem frees the allocated memory, but the "test" string does NOT get freed
  // this is at least a memory leak, sometimes it can even result in a crash
  FreeMem(pStrVar);
end;

procedure Proc3;
var pStrVar : TPString;
begin
  // "AllocMem" always initializes any allocated buffer
  pStrVar := AllocMem(sizeOf(string));

  // so the string assignment makes no problems
  pStrVar^ := 'test';

  // this time we reset the "pStrVar^" variable, so Delphi kills the string
  Finalize(pStrVar);

  // now we can free the memory, no memory leak anymore
  FreeMem(pStrVar);
end;

procedure Proc4;
begin
  // "New" knows that "pStrVar^" needs to be initialized and so does it automatically
  New(pStrVar);

  // so again no problem with the string assignment
  pStrVar^ := 'test';

  // "Dispose" knows that "pStrVar^" needs to be reset and so does it
  // after that it also frees the allocated memory
  Dispose(pStrVar);
end;