Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-05

assigning ansistring with shortstring

10 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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???

DarekM
to L505

L505 napisał(a):

Show quoted text 12 lines
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];

Show quoted text 28 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Darek

Jonas Maebe
to L505: I'm lacking some memory concept here. Below program doesn't work unless setlengths are…
L505 wrote 20 lines
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

L505
to Jonas Maebe: The length bytes of those shortstrings aren't initialised if you don't do setlength(). It…
Show quoted text 25 lines
> 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 :-)

Jonas Maebe
to L505
L505 wrote 9 lines
> 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

Arne Hanssen
to L505: I'm lacking some memory concept here. Below program doesn't work unless setlengths are…
L505 wrote 7 lines
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 ) )_ ) /~-----' ---------------------------------'---^~(_/-_)--(_/_)-------
Jonas Maebe
to Arne Hanssen
Arne Hanssen wrote 9 lines
> 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

L505
to Jonas Maebe
Show quoted text 16 lines
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).

Jonas Maebe
to L505
L505 wrote 9 lines
> 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