string in fpc

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

string in fpc

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

Re: string in fpc

Jonas Maebe-2

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]".

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

Re: string in fpc

Marco van de Voort
> 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>

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

Re: string in fpc

Michael Van Canneyt


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]

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

Re: string in fpc

Marco van de Voort
> 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?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string in fpc

Jonas Maebe-2

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".


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

Re: string in fpc

Marco van de Voort
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string in fpc

Felipe Monteiro de Carvalho
On 2/11/06, Marco van de Voort <[hidden email]> wrote:
> I mean including the possibilties to have non-255 sizes.

var x : string[4];

I suppose?

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

Re: string in fpc

Marco van de Voort
> On 2/11/06, Marco van de Voort <[hidden email]> 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].

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

Re: string in fpc

Michael Van Canneyt
In reply to this post by Marco van de Voort


On Sat, 11 Feb 2006, Marco van de Voort wrote:

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

Re: string in fpc

Michael Van Canneyt
In reply to this post by Marco van de Voort


On Sat, 11 Feb 2006, Marco van de Voort wrote:

> > On 2/11/06, Marco van de Voort <[hidden email]> 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].

TP is correct. Ansistring[4] is not valid, never...

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