Sign in Join

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

Mailing lists / fpc-pascal / 2011-11

pchar with more than 255 characters

27 messages · 10 people · –

Reply by email Subscribe to this list Your subscription

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

Rainer Stratmann
procedure getchars( p : pchar );
var l : longint;
begin
 l := length( p );
end;

If calling getchars with chars longer than 255 l is always 255.

For example:
getchars( '12345 ... 255256257258' +
'1234567213939873987' +
...
...

                '1234567213939873987');  // more than 255 chars

Version 2-4-2.0

With other Versions before 2-4-2.0 it was ok (more chars than 255 possible). It seems that this is a bug.
May be it is resolved already.

Reply by email

Rainer Stratmann
to leledumbo
leledumbo wrote
It works when {$LONGSTRINGS ON} or {$H+} directive exists. I think the compiler treats functions receiving array of char (or pointer to it) as ShortString when none of the directives above exist.

No it does not work then.
I put already the -Sh- additional compiler arg.

For me it seems like a bug, because in the previous version of FPC (2-2-4.3) it works. It seems that the compiler now cuts every constant string in the sourcecode to 255 characters.

Reply by email

Graeme Geldenhuys
to leledumbo: It works when {$LONGSTRINGS ON} or {$H+} directive exists. I think the compiler treats…
leledumbo wrote
I think the compiler treats functions receiving array of char (or pointer to it) as ShortString when none of the directives above exist.

Indeed, FPC defaults to ShortString if {$H+} is not specified. That is probably the problem. I really don't know why this is still so (maybe some performance thing inside the compiler itself). I now simply specify $H+ in the fpc.cfg file, in case I forget to add it to the header of a unit.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Jonas Maebe
to Rainer Stratmann: No it does not work then. I put already the -Sh- additional compiler arg. For me it seems…
Rainer Stratmann wrote 7 lines
Am Sunday 13 November 2011 02:15:07 schrieb leledumbo: > It works when {$LONGSTRINGS ON} or {$H+} directive exists. I think the > compiler treats functions receiving array of char (or pointer to it) as > ShortString when none of the directives above exist. No it does not work then. I put already the -Sh- additional compiler arg.

-Sh- Is the same as {$h-} or {$longstrings off}. You need -Sh (provided that there's no {$mode fpc/objfpc/tp/macpas} in the source file, since that will disable them again).

Jonas

Reply by email

Felipe Monteiro de Carvalho
to Rainer Stratmann: procedure getchars( p : pchar ); var l : longint; begin l := length( p ); end; If calling…
Rainer Stratmann wrote 5 lines
procedure getchars( p : pchar ); var l : longint; begin  l := length( p ); end;

You cannot do length in a PChar, it has no length information. I think this code is probably automatically converting the PChar to a Pascal String and then after that calling Length in the Pascal String, so your code is not very efficient.

Pass the length of the PChar in an aditional parameter instead. Or write a routine which will count until the first #0 of the PChar to see how many characters it has.

Felipe Monteiro de Carvalho

Reply by email

Sven Barth
to Graeme Geldenhuys: Indeed, FPC defaults to ShortString if {$H+} is not specified. That is probably the…
Graeme Geldenhuys wrote 10 lines
On 13/11/2011, leledumbo wrote: > I think the > compiler treats functions receiving array of char (or pointer to it) as > ShortString when none of the directives above exist. Indeed, FPC defaults to ShortString if {$H+} is not specified. That is probably the problem. I really don't know why this is still so (maybe some performance thing inside the compiler itself). I now simply specify $H+ in the fpc.cfg file, in case I forget to add it to the header of a unit.

That the compiler uses ShortString for performance reasons is not related to the fact that ObjFPC uses them by default. In this case the reason is simply backwards compatibilty (note: not Delphi compatibilty :P ). Though its not the compiler which is the problem here as it has an explicit "{$H-}" in its define include file.

Regards,
Sven

Reply by email

Graeme Geldenhuys
to Sven Barth
Sven Barth wrote
In this case the reason is simply backwards compatibilty (note: not Delphi compatibilty :P ).

But that's my point. I really don't think anybody is writing TP style programs any more. So wouldn't it make sense to change the default for the vast majority or programs written with FPC today? Mode TP (-Mtp) can default to shortstring and mode Delphi or mode ObjFPC can default to longstrings?

Just like I think FPC should default to mode ObjFPC (instead of mode fpc). Though I must admit, I have no idea what the difference are between those modes.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Sven Barth
to Graeme Geldenhuys
Graeme Geldenhuys wrote 10 lines
On 15/11/2011, Sven Barth wrote: > In this case the reason is simply backwards compatibilty (note: not Delphi > compatibilty :P ). But that's my point. I really don't think anybody is writing TP style programs any more. So wouldn't it make sense to change the default for the vast majority or programs written with FPC today? Mode TP (-Mtp) can default to shortstring and mode Delphi or mode ObjFPC can default to longstrings?

The backwards compatibilty in this case is not for TP, but for FPC code that uses ObjFPC, but not H+.

In the end you need to discuss this with Florian, Jonas or some other senior dev who has the final say for this...

Note: mode Delphi already defaults to H+

Just like I think FPC should default to mode ObjFPC (instead of mode fpc). Though I must admit, I have no idea what the difference are between those modes.

No classes, no try/except/finally and some other things maybe

Regards,
Sven

Reply by email

Rainer Stratmann
to Jonas Maebe: -Sh- Is the same as {$h-} or {$longstrings off}. You need -Sh (provided that there's no…
Jonas Maebe wrote 12 lines
On 13 Nov 2011, at 14:02, Rainer Stratmann wrote: > Am Sunday 13 November 2011 02:15:07 schrieb leledumbo: >> It works when {$LONGSTRINGS ON} or {$H+} directive exists. I think the >> compiler treats functions receiving array of char (or pointer to it) as >> ShortString when none of the directives above exist. > > No it does not work then. > I put already the -Sh- additional compiler arg. -Sh- Is the same as {$h-} or {$longstrings off}. You need -Sh (provided that there's no {$mode fpc/objfpc/tp/macpas} in the source file, since that will disable them again).

With {$h+} it works.

But when having pchar I wonder why it generally is not possible to make strings longer than 255 as it was possible in the previous compiler version.

For shortsting I accept strings not longer than 255 chars.

Why is it not possible to make a var like
svar : shortstring[ 20 ] ?

The whole program I made is compiled with {$h-} so it would be much fault-prone work to put everywhere {$h+} and then {$h-} again.

Reply by email

Martin
to Rainer Stratmann
Rainer Stratmann wrote 8 lines
-Sh- Is the same as {$h-} or {$longstrings off}. You need -Sh (provided that there's no {$mode fpc/objfpc/tp/macpas} in the source file, since that will disable them again). With {$h+} it works. But when having pchar I wonder why it generally is not possible to make strings longer than 255 as it was possible in the previous compiler version.

Maybe not an answer to your question, but why do you use length?

As mentioned before by someone else: length does read a field from the string, which does not exist in pchar (hence string can contain #0 and pchar can not).

So why not use pos(#0, foo_pchar) ?

Of course this does not answer the question IF or WHY this has changed

Reply by email

Unknown author
to Rainer Stratmann: With {$h+} it works. But when having pchar I wonder why it generally is not possible to…
Rainer Stratmann wrote 23 lines
Am Monday 14 November 2011 22:00:28 schrieb Jonas Maebe: > On 13 Nov 2011, at 14:02, Rainer Stratmann wrote: >> Am Sunday 13 November 2011 02:15:07 schrieb leledumbo: >>> It works when {$LONGSTRINGS ON} or {$H+} directive exists. I think the >>> compiler treats functions receiving array of char (or pointer to it) as >>> ShortString when none of the directives above exist. >> >> No it does not work then. >> I put already the -Sh- additional compiler arg. > > -Sh- Is the same as {$h-} or {$longstrings off}. You need -Sh (provided > that there's no {$mode fpc/objfpc/tp/macpas} in the source file, since that > will disable them again). With {$h+} it works. But when having pchar I wonder why it generally is not possible to make strings longer than 255 as it was possible in the previous compiler version. For shortsting I accept strings not longer than 255 chars. Why is it not possible to make a var like svar : shortstring[ 20 ] ?

Because the definition of the type 'ShortString':

   ShortString = String[255];

is somewhere in the ObjPas or system unit.

If the compiler finds a size after the 'String' keyword, it automatically assumes a "shortstring" (by this I mean the compiler internal's idea of "shortstring").

The definition of 'Shortstring' as shown above is just an aid, for Delphi compatibility.

Michael.

Reply by email

Sven Barth
to Rainer Stratmann: With {$h+} it works. But when having pchar I wonder why it generally is not possible to…
Rainer Stratmann wrote
But when having pchar I wonder why it generally is not possible to make strings longer than 255 as it was possible in the previous compiler version.

There was never a change regarding this as far as I know.

Show quoted text 5 lines
For shortsting I accept strings not longer than 255 chars. Why is it not possible to make a var like svar : shortstring[ 20 ] ?

Because the correct declaration is

svar: String[20];

This will give you a string with maximum length 20 (the maximum available length for a declaration like this is 255 which is equal to ShortString).

Regards,
Sven

Reply by email

Sven Barth
to Felipe Monteiro de Carvalho: But why is it limited to 255 chars in {$H-} ? (I understood that it is limited like this…
Felipe Monteiro de Carvalho wrote 5 lines
On Tue, Nov 15, 2011 at 11:33 AM, Jonas Maebe wrote: > length(pchar) is translated into strlen(pchar) (just like in Delphi). But why is it limited to 255 chars in {$H-} ? (I understood that it is limited like this from the original poster)

If you look at the call the original poster sent, you will see why:

Show quoted text 6 lines
For example: getchars( '12345 ... 255256257258' + '1234567213939873987' + ... ... '1234567213939873987'); // more than 255 chars

FPC converts the constant string to the current generic string type which is String[255] in case of {$H-} and not AnsiString. Thus the string will appear truncated after 255 characters.

Regards,
Sven

Reply by email

Jonas Maebe
to Sven Barth: There was never a change regarding this as far as I know. Because the correct declaration…
Sven Barth wrote 7 lines
Am 16.11.2011 16:55, schrieb Rainer Stratmann: > But when having pchar I wonder why it generally is not possible to > make > strings longer than 255 as it was possible in the previous compiler > version. There was never a change regarding this as far as I know.

There is one change about this in 2.6.0: http://wiki.freepascal.org/User_Changes_2.6.0#String_constants_longer_than_255_chars

Jonas

Reply by email

Jonas Maebe
to Felipe Monteiro de Carvalho: But why is it limited to 255 chars in {$H-} ? (I understood that it is limited like this…
Felipe Monteiro de Carvalho wrote 5 lines
On Tue, Nov 15, 2011 at 11:33 AM, Jonas Maebe < > wrote: > length(pchar) is translated into strlen(pchar) (just like in Delphi). But why is it limited to 255 chars in {$H-} ?

It is not.

(I understood that it is limited like this from the original poster)

The original poster said that he could not use string constants longer than 255 characters in his code in {$h-} mode.

Jonas

Reply by email

Sven Barth
to Jonas Maebe: There is one change about this in 2.6.0: http://wiki.freepascal.org/User_Changes_2.6.0#Str…
Jonas Maebe wrote 12 lines
On 16 Nov 2011, at 17:15, Sven Barth wrote: > Am 16.11.2011 16:55, schrieb Rainer Stratmann: >> But when having pchar I wonder why it generally is not possible to make >> strings longer than 255 as it was possible in the previous compiler >> version. > > There was never a change regarding this as far as I know. There is one change about this in 2.6.0: http://wiki.freepascal.org/User_Changes_2.6.0#String_constants_longer_than_255_chars

Thanks for pointing out. Nevertheless Rainer said that he has that problem with 2.4.2 (at least according to his first mail).

Regards,
Sven

Reply by email

Sven Barth
to Jonas Maebe: There is one change about this in 2.6.0: http://wiki.freepascal.org/User_Changes_2.6.0#Str…
Jonas Maebe wrote 12 lines
On 16 Nov 2011, at 17:15, Sven Barth wrote: > Am 16.11.2011 16:55, schrieb Rainer Stratmann: >> But when having pchar I wonder why it generally is not possible to make >> strings longer than 255 as it was possible in the previous compiler >> version. > > There was never a change regarding this as far as I know. There is one change about this in 2.6.0: http://wiki.freepascal.org/User_Changes_2.6.0#String_constants_longer_than_255_chars

(I should have read the listed change before clicking "Send")

So using 2.6.0+ Rainer would at least now know that he has a constant string > 255 in mode $H- and that this would have been truncated before.

Regards,
Sven

Reply by email

Rainer Stratmann
to Felipe Monteiro de Carvalho: But why is it limited to 255 chars in {$H-} ? (I understood that it is limited like this…
Felipe Monteiro de Carvalho wrote
On Tue, Nov 15, 2011 at 11:33 AM, Jonas Maebe
> length(pchar) is translated into strlen(pchar) (just like in Delphi). But why is it limited to 255 chars in {$H-} ? (I understood that it is limited like this from the original poster)

Exactly.
Why?

Now I changed all string vars (aprox. 1250) to shortstring and the string[..] vars remain the same as before.

So now I can remove the swith -Sh- from additional compiler arguments/flags. And now it is possible again (like in the compiler versions before) to put more than 255 chars incombination with a pchar.

Reply by email

Tomas Hajny
to Bart: Couldn't resist; Turbo Pascal 6.0: Program Prog; uses objects; var pc: PChar; Len:…
Bart wrote 21 lines
On 11/16/11, Jonas Maebe wrote: > Again: length(pchar) is perfectly correct and will be translated into > strlen(pchar), just like in Delphi (and probably even like in TP). Couldn't resist; Turbo Pascal 6.0: Program Prog; uses objects; var pc: PChar; Len: Integer; begin Len := Length(pc|); end. Error 67: String expression expected. (| indicates the cursor)

Indeed - TP/BP 7.0 gives the same (error) result. Unit strings (which might be more appropriate than unit objects included by Bart) doesn't change the result either.

Tomas

Reply by email