[fpc-devel] Strange Problem!

Amir amir at aavani.net
Wed Apr 25 00:36:02 CEST 2012


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



More information about the fpc-devel mailing list