[fpc-devel] Strange Problem!

Flávio Etrusco flavio.etrusco at gmail.com
Wed Apr 25 13:25:23 CEST 2012


You program isn't valid. You're assigning a constant (a global
variable) to Result, so you shouldn't change it's contents through a
pointer (if you change it normally the compiler+RTL will COW the
value).
You can use a shortstring or:
Result := StringOfChar(' ', 16);

-Flávio

On Tue, Apr 24, 2012 at 7:36 PM, Amir <amir at aavani.net> wrote:
> Hi,
>
> I have encountered a strange problem. I developed the following function as
> a faster implementation for IntToStr:
>
>
> function MyIntToStr (n: Int64): AnsiString;
> const
>  MaxLength= 16;
>
> var
>  CurDigit: PChar;
>  Sign: Boolean;
>
> begin
>  Result:= '                ';
>
>  CurDigit:= @(Result [MaxLength]);
>  Sign:= False;
>
>  if n< 0 then
>  begin
>    Sign:= True;
>    n:= -n;
>
>  end;
>  if n= 0 then
>    Exit ('0');
>
>
>  while n<> 0 do
>  begin
>    CurDigit^:= Char (48+ n mod 10);
>    Dec (CurDigit);
>    n:= n div 10;
>
>  end;
>  if Sign then
>    CurDigit^:= '-';
>
> end;
>
> And then, in main , I have
>
> begin
>  WriteLn (MyIntToStr (5458));
>  WriteLn (MyIntToStr (5458));
>  WriteLn (MyIntToStr (2));
>
> end.
>
> The output is:
>           5458
>           5458
>           5452
>
> I tried gdb and noticed the first line of MyIntToStr (Result:= '...') is not
> executed correctly. I am using fpc-2.6.0-1.x86_64
>
>
> Thanks,
> Amir
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel



More information about the fpc-devel mailing list