[fpc-devel] Strange Problem!

Amir amir at aavani.net
Wed Apr 25 22:11:24 CEST 2012


I am not sure what you mean be "your program is not valid". I am 
initializing Result. Modify its content and ...

I already observed that if I use either shortstring or use indices to 
modify Result, this problem does not occur. But still I can't see what 
causes this problem. And I believe it is a bug, isn't it?

Amir

On 04/25/2012 04:25 AM, Flávio Etrusco wrote:
> 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