Sign in Join

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

Mailing lists / fpc-pascal / 2006-02

string in fpc

11 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Andrzej Baran

Hello,
I noticed that declaration of variable like:
a:string[1]; is not valid any more in fpc-2.0.2, so how can I declare a variable to get few char string instead of full length

thanks
Andy

Jonas Maebe
to Andrzej Baran
Andrzej Baran wrote
I noticed that declaration of variable like: a:string[1]; is not valid any more in fpc-2.0.2, so how can I declare a variable to get few char string instead of full length

It works here, even if ansistrings are the default ({$h+} or Delphi mode activated). The only thing that (logically) doesn't work here is "a:ansistring[1]".

Note that 2.02 has a bug whereby reading such length-limited strings can cause a buffer overflow (which has been fixed in the mean time).

Jonas

Marco van de Voort
to Jonas Maebe
Show quoted text 10 lines
On 10 Feb 2006, at 20:31, Andrzej Baran wrote: > I noticed that declaration of variable like: > a:string[1]; is not valid any more in fpc-2.0.2, so how can I > declare a > variable to get few char string instead of full length It works here, even if ansistrings are the default ({$h+} or Delphi mode activated). The only thing that (logically) doesn't work here is "a:ansistring[1]".

This also doesn't work (I don't see why not?)

{$mode delphi}
var x : shortstring[4];
begin
end.

[Snail] </home/marcov/testing> fpc testshort
Free Pascal Compiler version 2.1.1 [2006/02/10] for i386 Copyright (c) 1993-2005 by Florian Klaempfl
Target OS: FreeBSD/ELF for i386
Compiling testshort.pp
testshort.pp(5,23) Error: Error in type definition testshort.pp(8,4) Fatal: There were 1 errors compiling module, stopping Fatal: Compilation aborted
Error: /home/marcov/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
[Snail] </home/marcov/testing>

Michael Van Canneyt
to Marco van de Voort
Marco van de Voort wrote 19 lines
> On 10 Feb 2006, at 20:31, Andrzej Baran wrote: > > > I noticed that declaration of variable like: > > a:string[1]; is not valid any more in fpc-2.0.2, so how can I > > declare a > > variable to get few char string instead of full length > > It works here, even if ansistrings are the default ({$h+} or Delphi > mode activated). The only thing that (logically) doesn't work here is > "a:ansistring[1]". This also doesn't work (I don't see why not?) {$mode delphi} var x : shortstring[4]; begin end.

Because shortstring is defined as String[255]

Michael.

Marco van de Voort
to Michael Van Canneyt
Show quoted text 23 lines
On Sat, 11 Feb 2006, Marco van de Voort wrote: > > On 10 Feb 2006, at 20:31, Andrzej Baran wrote: > > > > > I noticed that declaration of variable like: > > > a:string[1]; is not valid any more in fpc-2.0.2, so how can I > > > declare a > > > variable to get few char string instead of full length > > > > It works here, even if ansistrings are the default ({$h+} or Delphi > > mode activated). The only thing that (logically) doesn't work here is > > "a:ansistring[1]". > > This also doesn't work (I don't see why not?) > > {$mode delphi} > > var x : shortstring[4]; > > begin > end. Because shortstring is defined as String[255]

Hmm. How do I get real shortstring behaviour in delphi mode then?

Jonas Maebe
to Marco van de Voort
Marco van de Voort wrote
> Because shortstring is defined as String[255] Hmm. How do I get real shortstring behaviour in delphi mode then?

A string[255] is identical to a "real shortstring".

Jonas

Marco van de Voort
to Jonas Maebe
Show quoted text 7 lines
On 11 Feb 2006, at 17:40, Marco van de Voort wrote: >> Because shortstring is defined as String[255] > > Hmm. How do I get real shortstring behaviour in delphi mode then? A string[255] is identical to a "real shortstring".

I mean including the possibilties to have non-255 sizes.

Marco van de Voort
to Felipe Monteiro de Carvalho
Show quoted text 6 lines
On 2/11/06, Marco van de Voort wrote: > I mean including the possibilties to have non-255 sizes. var x : string[4]; I suppose?

Apparantly so yes. Assumed that would equal to ansistring[4], but it does to tp string[4].

Michael Van Canneyt
to Marco van de Voort: I mean including the possibilties to have non-255 sizes.
Marco van de Voort wrote 9 lines
> On 11 Feb 2006, at 17:40, Marco van de Voort wrote: > > >> Because shortstring is defined as String[255] > > > > Hmm. How do I get real shortstring behaviour in delphi mode then? > > A string[255] is identical to a "real shortstring". I mean including the possibilties to have non-255 sizes.

Simple:

Var
  A : String[10];
{$H+} just means that for
  A : String;

A is an ansistring.

{$H-} means that for
  A : String;

A is a 255 char string, TP style, and hence is equivalent to

A : ShortString

As soon as there is a size indication, it's a TP-style string.

Michael.