Иллюстрация кода
procedure TForm1.FormCreate(Sender: TObject);
var
Str: string;
PStr: PChar;
begin
Str := 'This a string.';
PStr := Pointer(Str); // PStr holds the address of the first char of Str
ShowMessage(IntToStr(Longint(PStr))); // It displays e.g. 4928304
Setlength(Str, 20);
// I don't know what actually happens in the call for SetLength() above,
// because the address of Str changes now, so the PStr not valid anymore.
// This is a proof of the fact
PStr := Pointer(Str);
ShowMessage(IntToStr(Longint(PStr))); // It's now different, e.g. 11423804
end;
Вопрос
- Почему
System.SetLength (Str, Len)
вызывает адрес Str изменений?
- Есть ли способ аннулировать этот побочный эффект
SetLength
, чтобы мне не пришлось переназначать новый адрес Str на PStr ]?
задан Astaroth 22 December 2010 в 15:23
поделиться