[fpc-pascal] assigning ansistring with shortstring
L505
fpc505 at z505.com
Fri May 26 19:35:09 CEST 2006
> > 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 :-)
More information about the fpc-pascal
mailing list