Sign in Join

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

Mailing lists / fpc-pascal / 2007-06

Re: Version 2.1.4

11 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Rainer Stratmann

Hello,

1.

var s:string;
...
s[1]:='#';   -->   causes an access violation

2.

Why is it no more possible to assign the length index s[0] in a string? I find that is too much security to supress that.
It is cutting responsibility from the programmers.

Kind regards,
Rainer

Peter Vreman
to Rainer Stratmann
you wrote 13 lines
Hello, 1. var s:string; ... s[1]:='#'; --> causes an access violation 2. Why is it no more possible to assign the length index s[0] in a string? I find that is too much security to supress that. It is cutting responsibility from the programmers.

You are using ansistrings instead of shortstrings. Use SetLength() first to allocate memory.

Peter

Florian Klaempfl
to Rainer Stratmann: Does that mean that var s:string; is definig an ansistring in delphi mode? In the…
Show quoted text 23 lines
Am Samstag, 2. Juni 2007 19:59 schrieb Peter Vreman: > At 19:58 2-6-2007, you wrote: >> Hello, >> >> 1. >> >> var s:string; >> ... >> s[1]:='#'; --> causes an access violation >> >> 2. >> >> Why is it no more possible to assign the length index s[0] in a string? >> I find that is too much security to supress that. >> It is cutting responsibility from the programmers. > You are using ansistrings instead of shortstrings. Use SetLength() > first to allocate memory. > Does that mean that var s:string; is definig an ansistring in delphi mode? In the previous version this works,

Unlikely. string meant for years ansistring in objfpc/delphi mode.

is it now changing generally to ansistrings? How can I assign "normal" pascalstrings?

shortstring

Show quoted text 14 lines
Is there a description about the behaviour of strings and ansistrings in the various modes? Kind regards, Rainer > Peter > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Rainer Stratmann
to Peter Vreman: You are using ansistrings instead of shortstrings. Use SetLength() first to allocate…
Peter Vreman wrote 18 lines
At 19:58 2-6-2007, you wrote: >Hello, > >1. > >var s:string; >... >s[1]:='#'; --> causes an access violation > >2. > >Why is it no more possible to assign the length index s[0] in a string? >I find that is too much security to supress that. >It is cutting responsibility from the programmers. You are using ansistrings instead of shortstrings. Use SetLength() first to allocate memory.

Does that mean that

var s:string; is definig an ansistring in delphi mode?

In the previous version this works, is it now changing generally to ansistrings?
How can I assign "normal" pascalstrings?
Is there a description about the behaviour of strings and ansistrings in the various modes?

Kind regards,
Rainer

Show quoted text 6 lines
Peter _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Florian Klaempfl
to Rainer Stratmann: what does var s:string[12]; in delphimode mean? ansistring or shortstring? if ansistring,…
Show quoted text 12 lines
Am Samstag, 2. Juni 2007 20:16 schrieb Florian Klaempfl: >> How can I assign "normal" pascalstrings? > shortstring > what does var s:string[12]; in delphimode mean? ansistring or shortstring? if ansistring, how can I get access to a "normal" pascal (short)string with maximum length of 12?

string[12] ?

Show quoted text 9 lines
var s:shortstring[12]; does not work in delphi mode. Rainer _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Michael Van Canneyt
to Rainer Stratmann: Does that mean that var s:string; is definig an ansistring in delphi mode? In the…
Rainer Stratmann wrote 22 lines
Am Samstag, 2. Juni 2007 19:59 schrieb Peter Vreman: > At 19:58 2-6-2007, you wrote: > >Hello, > > > >1. > > > >var s:string; > >... > >s[1]:='#'; --> causes an access violation > > > >2. > > > >Why is it no more possible to assign the length index s[0] in a string? > >I find that is too much security to supress that. > >It is cutting responsibility from the programmers. > > You are using ansistrings instead of shortstrings. Use SetLength() > first to allocate memory. > Does that mean that var s:string; is definig an ansistring in delphi mode?

Yes.

In the previous version this works, is it now changing generally to ansistrings?

Yes.
In old versions {$mode delphi} did not automatically imply {$h+}. Now it does.

How can I assign "normal" pascalstrings?

Put

{$h-}

at the start of your file, or use the -Sh- option on the command-line.

Alternatively, define your string as
Var
  S : Shortstring;

This works in any mode, and is delphi compatible.

Is there a description about the behaviour of strings and ansistrings in the various modes?

Yes, the reference documentation. The only effect of the {$h} directive is to map String->Ansistring when {$h ON} or String-> Shortstring when {$h off} is in effect. You can always force either of those by explicitly using ansistring, shortstring or declaring a fixed-length string.

This is 100% delphi compatible. The only thing that has changed is that {$mode delphi} now implicitly implies {$h+}. This is again compatible with newer versions of Delphi, where {$H+} is now the default.

Hope this clarifies matters a bit. I'll add a statement of the changed behaviour to the documentation of {$mode} and {$H}

Michael.

Michael Van Canneyt
to Rainer Stratmann: what does var s:string[12]; in delphimode mean? ansistring or shortstring? if ansistring,…
Rainer Stratmann wrote 17 lines
Am Samstag, 2. Juni 2007 20:16 schrieb Florian Klaempfl: > > How can I assign "normal" pascalstrings? > > shortstring > what does var s:string[12]; in delphimode mean? ansistring or shortstring? if ansistring, how can I get access to a "normal" pascal (short)string with maximum length of 12? var s:shortstring[12]; does not work in delphi mode.

Obviously, since the definition of shortstring is:

  ShortString = String[255];

So actually your above statement is equivalent to writing

 Var s:string[255][12];

which, you'll agree, is wrong :-)

Michael.

Rainer Stratmann
to Florian Klaempfl: string[12] ?
Florian Klaempfl wrote 16 lines
Rainer Stratmann schrieb: > Am Samstag, 2. Juni 2007 20:16 schrieb Florian Klaempfl: >>> How can I assign "normal" pascalstrings? >> >> shortstring > > what does > > var s:string[12]; > > in delphimode mean? > ansistring or shortstring? > if ansistring, how can I get access to a "normal" pascal (short)string > with maximum length of 12? string[12] ?

ok, now I understand.

string means ansistring in delphi mode
and string[12] means shortstring with maximum length of 12 in delphi mode.

Thanks,
Kind regards
Rainer

Show quoted text 12 lines
> var s:shortstring[12]; > > does not work in delphi mode. > > Rainer > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Rainer Stratmann
to Michael Van Canneyt: Obviously, since the definition of shortstring is: ShortString = String[255]; So actually…
Michael Van Canneyt wrote 30 lines
On Sat, 2 Jun 2007, Rainer Stratmann wrote: > Am Samstag, 2. Juni 2007 20:16 schrieb Florian Klaempfl: > > > How can I assign "normal" pascalstrings? > > > > shortstring > > what does > > var s:string[12]; > > in delphimode mean? > ansistring or shortstring? > if ansistring, how can I get access to a "normal" pascal (short)string > with maximum length of 12? > > var s:shortstring[12]; > > does not work in delphi mode. Obviously, since the definition of shortstring is: ShortString = String[255]; So actually your above statement is equivalent to writing Var s:string[255][12]; which, you'll agree, is wrong :-) Michael.

OK, this part I understand, now, thanks for explanation. I'm not deep involved in compiler programming, but it is good to know that string[255] also works.

Now, I use the www.frameworkpascal.com for a DOS Program and ported it to Linux, so that the program can run with DOS and Linux. Let it run in DOS with fpc compiler seems more work, so I run the program in DOS with the frameworkcompiler.

I did it now with additional compiler args -Sh- and it works, thanks.

Kind regards,
Rainer