assigning ansistring with shortstring

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

assigning ansistring with shortstring

L505
I'm lacking some memory concept here. Below program doesn't work unless setlengths
are called first on the shortstrings.

program Project1;

{$mode objfpc}{$H+}

var
  filename: string;
  last3: string[3];
  last4: string[4];
begin
  filename:= 'test';
  filename:= filename+ 'ing.pas';
  setlength(last3, 3);
  last3[1]:= filename[length(filename)-2];
  last3[2]:= filename[length(filename)-1];
  last3[3]:= filename[length(filename)];
  writeln('debug last3: ' + last3);
  setlength(last4, 4);
  last4[1]:= filename[length(filename)-3];
  last4[2]:= filename[length(filename)-2];
  last4[3]:= filename[length(filename)-1];
  last4[4]:= filename[length(filename)];
  writeln('debug last4: ' + last4);
  readln;
end.


I'd never have thought to use a setlength on a shortstring???




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

Dariusz Mazur
L505 napisaƂ(a):

> I'm lacking some memory concept here. Below program doesn't work unless setlengths
> are called first on the shortstrings.
>
> program Project1;
>
> {$mode objfpc}{$H+}
>
> var
>   filename: string;
>   last3: string[3];
>   last4: string[4];
>  
this is not short strings
use
last3: shortstring[3];


> begin
>   filename:= 'test';
>   filename:= filename+ 'ing.pas';
>   setlength(last3, 3);
>   last3[1]:= filename[length(filename)-2];
>   last3[2]:= filename[length(filename)-1];
>   last3[3]:= filename[length(filename)];
>   writeln('debug last3: ' + last3);
>   setlength(last4, 4);
>   last4[1]:= filename[length(filename)-3];
>   last4[2]:= filename[length(filename)-2];
>   last4[3]:= filename[length(filename)-1];
>   last4[4]:= filename[length(filename)];
>   writeln('debug last4: ' + last4);
>   readln;
> end.
>
>
> I'd never have thought to use a setlength on a shortstring???
>
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>  
Darek
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

Jonas Maebe-2
In reply to this post by L505

On 26 May 2006, at 14:10, L505 wrote:

> I'm lacking some memory concept here. Below program doesn't work  
> unless setlengths
> are called first on the shortstrings.
>
> program Project1;
>
> {$mode objfpc}{$H+}
>
> var
>   filename: string;
>   last3: string[3];
>   last4: string[4];
> begin
>   filename:= 'test';
>   filename:= filename+ 'ing.pas';
>   setlength(last3, 3);
>   last3[1]:= filename[length(filename)-2];
>   last3[2]:= filename[length(filename)-1];
>   last3[3]:= filename[length(filename)];
>   writeln('debug last3: ' + last3);

The length bytes of those shortstrings aren't initialised if you  
don't do setlength(). It has nothing to do with memory allocation,  
but with initialisation.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

L505
In reply to this post by Dariusz Mazur
> > var
> >   filename: string;
> >   last3: string[3];
> >   last4: string[4];
> >  
> this is not short strings
> use
> last3: shortstring[3];

In delphi 5 and FPC  in objfpc mode it doesn't accept that.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

L505
In reply to this post by Jonas Maebe-2
> > I'm lacking some memory concept here. Below program doesn't work
> > unless setlengths
> > are called first on the shortstrings.
> >
> > program Project1;
> >
> > {$mode objfpc}{$H+}
> >
> > var
> >   filename: string;
> >   last3: string[3];
> >   last4: string[4];
> > begin
> >   filename:= 'test';
> >   filename:= filename+ 'ing.pas';
> >   setlength(last3, 3);
> >   last3[1]:= filename[length(filename)-2];
> >   last3[2]:= filename[length(filename)-1];
> >   last3[3]:= filename[length(filename)];
> >   writeln('debug last3: ' + last3);
>
> The length bytes of those shortstrings aren't initialised if you
> don't do setlength(). It has nothing to do with memory allocation,
> but with initialisation.
>

Learn something new today - I unfortunately don't have a turbopascal background and in
this case it would really help - I guess I made the silly assumption that shortstrings
were ready to go without ever needed a setlength. Of course I had the nerve to turn off
range checking and try accessing outside of 255 bounds but it didn't work, as expected :-)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

Jonas Maebe-2

On 26 May 2006, at 19:35, L505 wrote:

>> The length bytes of those shortstrings aren't initialised if you
>> don't do setlength(). It has nothing to do with memory allocation,
>> but with initialisation.
>
> Learn something new today - I unfortunately don't have a  
> turbopascal background and in
> this case it would really help - I guess I made the silly  
> assumption that shortstrings
> were ready to go without ever needed a setlength.

They are, as long as you use regular string assignments,  
concatenations etc. Otherwise I'm not sure how the compiler/rtl is  
supposed how long your string actually is.


Jonas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

Arne Hanssen-2
In reply to this post by L505
L505 wrote:
> I'm lacking some memory concept here. Below program doesn't work unless setlengths
> are called first on the shortstrings.
>
>   setlength(last3, 3);
>   last3[1]:= filename[length(filename)-2];
>   last3[2]:= filename[length(filename)-1];
>   last3[3]:= filename[length(filename)];

As already explained, you must tell 'last3' its length.  As you already
are using a somewhat "lowlevel" approach, you could replace the state-
ment 'setlength(last3, 3);' with 'last3[0]:=chr(3);'.

--
Vennlig hilsen / Best regards      |\     ___,,--,        _
Arne Hanssen, Senja, Norway        /,`--''        \-,,__,'/
http://www.tuxic.net/             |,4   ) )_    ) /~-----'
---------------------------------'---^~(_/-_)--(_/_)-------
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

Jonas Maebe-2

On 27 May 2006, at 09:46, Arne Hanssen wrote:

>>   setlength(last3, 3);
>>   last3[1]:= filename[length(filename)-2];
>>   last3[2]:= filename[length(filename)-1];
>>   last3[3]:= filename[length(filename)];
>
> As already explained, you must tell 'last3' its length.  As you  
> already
> are using a somewhat "lowlevel" approach, you could replace the state-
> ment 'setlength(last3, 3);' with 'last3[0]:=chr(3);'.

Note that the setlength will generate exactly the same code as the  
'last3[0]:=chr(3);' statement, so it's better to keep the setlength  
(in case the string would ever become an ansistring, or just for  
readability)


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

L505

> On 27 May 2006, at 09:46, Arne Hanssen wrote:
>
> >>   setlength(last3, 3);
> >>   last3[1]:= filename[length(filename)-2];
> >>   last3[2]:= filename[length(filename)-1];
> >>   last3[3]:= filename[length(filename)];
> >
> > As already explained, you must tell 'last3' its length.  As you
> > already
> > are using a somewhat "lowlevel" approach, you could replace the state-
> > ment 'setlength(last3, 3);' with 'last3[0]:=chr(3);'.
>
> Note that the setlength will generate exactly the same code as the
> 'last3[0]:=chr(3);' statement, so it's better to keep the setlength
> (in case the string would ever become an ansistring, or just for
> readability)

I didn't think the compiler would let you access string[0] directly anyway, even with
range checking off - since that's a compiler thing - but I haven't checked for sure.
(using i:=0; string[i] could work but calling setlength is fine for me for this purpose).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: assigning ansistring with shortstring

Jonas Maebe-2

On 27 May 2006, at 21:54, L505 wrote:

>> Note that the setlength will generate exactly the same code as the
>> 'last3[0]:=chr(3);' statement, so it's better to keep the setlength
>> (in case the string would ever become an ansistring, or just for
>> readability)
>
> I didn't think the compiler would let you access string[0] directly  
> anyway, even with
> range checking off - since that's a compiler thing - but I haven't  
> checked for sure.

It does allow it for shortstrings, just like TP did.


Jonas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal