Thanks for that explanation! BTW, I noticed that if you use the return
value of GetTempPath you can improve things further:
function GetTempDir:String;
const
MaxPathLength = 255;
var
L :Integer;
begin
SetLength(Result,MaxPathLength+1);
L := GetTempPath(MaxPathLength,PChar(Result));
if L<>0 then begin
if Result[L]<>'\' then begin
inc(L);
Result[L] := '\';
end;
SetLength(Result,L);
end
else Result := '';
end;
I tested it - it speeds up the function by a whole 1%! :) Have a good
weekend.
Cheers,
Carl
-----Original Message-----
From: Aaron Scott-Boddendijk [mailto:[EMAIL PROTECTED]]
Sent: Friday, 11 February 2000 16:00
To: Multiple recipients of list delphi
Subject: Re: [DUG]: Temp dir
> Why do you use the marked lines rather than going
> Result := Result + '\';
whoops - that came from an earlier arrangement to avoid a reallocation.
The trailing '\' correction should really be handled differently - try
function GetTempDir:String;
const
MaxPathLength = 255;
var
L :Integer;
begin
SetLength(Result,MaxPathLength+1);
if GetTempPath(MaxPathLength,PChar(Result))<>0 then begin
L := StrLen(PChar(Result));
if Result[L]<>'\' then begin
inc(L);
Result[L] := '\';
end;
SetLength(Result,L);
end
else Result := '';
end;
That now doesn't need a reallocation for providing a trailing '\'
> Doesn't using SetLength to make a string bigger cause a memory
reallocation
> anyway? Or did you mean to make GetTempDir a ShortString? And would it
be
> better if you did?
That depends... What's the maximum length that GetTempPath can return, if
MaxPathLength was defined in System or SysUtils it possible a future API
call
might accept larger than 255 character lengths... I don't think under this
case there's
any significant advantage but it might avoid some allocation at the expense
of a copy
on the other end rather than a reference count increase.
--
Aaron@home
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
application/ms-tnef