Recent

Author Topic: [Solved] Relationship between high(ashortstring) and length(ashortstring)  (Read 8913 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #45 on: September 12, 2022, 02:17:47 pm »
Quote
Correct, there is no warning and there should not be, because the compiler does not know that you set the length of the string to 18.

And with this he does not know?  :-\

Code: Pascal  [Select][+][-]
  1. var
  2.     Frase: String = 'Esto es una prueba';

The compiler knows that the constant 'Esto es una prueba' has a specific length, however you are assigning it to a variable (typed constant would be the same here by the way) and with that the compiler looses any knowledge about the length, because it's after all no longer a constant, but a variable and it might be changed from somewhere else. This is where my point regarding data flow analysis comes in: if the compiler can prove that Frase still contains the string assigned in the initialization then (and only then) it can do things based upon this knowledge. But, again, Data Flow Analysis is expensive. Thus it's only done in higher optimization cases and even then not everything might be covered yet (e.g. currently string constants might not be handled by the DFA and ConstProp optimizations, but they could).

Ha, ok, finalmente entendí, and now I am a true String guru, many thanks Sven.

 ;D

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #46 on: September 12, 2022, 03:15:36 pm »
I have still a question before to become Master-Guru es String.  :-[

About high(string) result in fpc vs Delphi.
Thanks to KodeZwerg to have tested it in Delphi:
https://forum.lazarus.freepascal.org/index.php/topic,60504.msg452762.html#msg452762

This in fpc:
Code: Pascal  [Select][+][-]
  1. program cadena;
  2. var
  3.     Frase: String ;
  4. begin
  5.   WriteLn(high(Frase));
  6. end.

gves as result: 255.

And same code in Delphi gives 0. (see picture)

I suppose it is because the fpc program, with default mode, uses alias String for ShortString and
Delphi uses alias String for AnsiString (using .Frase: AnsiString  gives 0 like Delphi).

True?
« Last Edit: September 12, 2022, 03:22:42 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

rvk

  • Hero Member
  • *****
  • Posts: 7017
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #47 on: September 12, 2022, 03:22:39 pm »
I suppose it is because the fpc program, with default mode, uses alias String for ShortString and
Delphi uses alias String for AnsiString (using .Frase: AnsiString  gives 0 like Delphi).
Almost. FPC defaults to OBJFPC and following that, String defaults to ShortString.
If you would put in {$MODE DELPHI}, even without specifying {$H+} or {$H-} the default of String would be AnsiString.
So the string-mode isn't a default of fpc itself, but the default of the chosen mode.

That's why it's always advised to specify {$H+} or {$H-} to eliminate confusion.
(and putting the mode in there is also advised for the same reason)

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #48 on: September 12, 2022, 03:34:29 pm »
Almost. FPC defaults to OBJFPC and following that, String defaults to ShortString.
If you would put in {$MODE DELPHI}, even without specifying {$H+} or {$H-} the default of String would be AnsiString.
So the string-mode isn't a default of fpc itself, but the default of the chosen mode.

That's why it's always advised to specify {$H+} or {$H-} to eliminate confusion.
(and putting the mode in there is also advised for the same reason)

Ok but... (be careful, I am nearly MasterGuru es String  ;))

Quote
Almost. FPC defaults to OBJFPC and

If you are using Lazarus, otherwise default mode is FPC:
https://www.freepascal.org/docs-html/user/userse33.html

But I agree with what you said.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #49 on: September 12, 2022, 03:39:32 pm »
Delphi uses alias String for AnsiString
True?
No and Yes.
Delphi 2009 and newer = string = WideString
Delphi lower than 2009 = string = AnsiString

I hope this helps but in fact it makes no difference on your subject.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #50 on: September 12, 2022, 04:14:42 pm »
Delphi uses alias String for AnsiString
True?
No and Yes.
Delphi 2009 and newer = string = WideString
Delphi lower than 2009 = string = AnsiString

I hope this helps but in fact it makes no difference on your subject.

Yes, it helps, tanks for the info. (important for become Master).

About result of Delphi vs fpc, there is a last thing I want to test but I dont have Delphi...

You did test with string = WideString ( you see I follow  ;) )

What is the result in Delphi with:
Code: Pascal  [Select][+][-]
  1.  var s:shortstring;

Do you also have 255 as result?

(In case of somebody boring like me ask it)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #51 on: September 12, 2022, 04:36:28 pm »
You did test with string = WideString ( you see I follow  ;) )
:D O:-) O:-) O:-) Yes i see it!!
And your request, i made a screenshot for you, any more wishes from side of Delphi that interest you?
« Last Edit: September 12, 2022, 04:42:15 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #52 on: September 12, 2022, 04:44:07 pm »
Code: Pascal  [Select][+][-]
  1.   s := ''; // strings need to be initialized, shortstrings not
  2.  
Quote from: Arioch
Do it? Managed types are auto-initialized to nil (and auto-destroyed when the function/program exits)[/quote

If your String is a result variable then you should initialize it nevertheless, cause due to how string results are passed to and from functions you might get another string in there.

I know that coan in Delphi. But i did not bring it (would be too much of offtopic and of shallow showing off before newbs).

And, that clearly was not a result variable.

However, i expected that FPC get rid of it. Delphi has no difference between out-params and var-params, at least for strings. FPC did make a technical difference. I was expecting FPC fixed it. Afterall bug-compatibility was not FPC's goal, was it...

Quote
The OS functions won't cancel that, because the same functions are also used for writing binary data which can contain #0 as well. What might happen however is that your terminal might not display data beyond the first #0.

This was my idea too, that's why i was carefulyl wording to show OS as a whole, not specific layers of it :-)
Terminal output is OS too. So is output pipelining. And what not.

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #53 on: September 12, 2022, 04:46:41 pm »
You did test with string = WideString ( you see I follow  ;) )
:D O:-) O:-) O:-) Yes i see it!!
And your request, i made a screenshort for you, any more wishes from side of Delphi that interest you?
Perfect!

And with "ShortString: 255", I like it!  ;D

Quote
any more wishes from side of Delphi that interest you?

No, at the moment I dont see, many thanks.
(And excellent news, I can become Master-Guru es String without to buy+install Delphi.)

Thanks KodeZwerg.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #54 on: September 12, 2022, 04:50:35 pm »
Delphi 2009 and newer = string = WideString
Delphi lower than 2009 = string = AnsiString

Wrong! 

string = UnicodeString  // 2009+ of course, and given {$H+} of course

WideString is Microsoft BSTR, and it does not have ref-counting, like Delphi-native long strings. Hence there is no COW but a COA instead.
UnicodeString is then a Delphi-like long string with ARC and COW

I think this difference in performance balance and memory use was important enough to make new data type rather then re-use Microsoft type.

WideString also probably does not have codepage (or at least is kept outisde COM BSTR frame and is invisible for non-Delphi COM pograms)

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #55 on: September 12, 2022, 05:05:11 pm »
[ any more wishes from side of Delphi that interest you?

Sorry, it is already still me.
Yes, there is one last wish for shortstring.  :-[
But it is the last, I promise.

What is the result of length(s) with shortstring in Delphi.
Code: Pascal  [Select][+][-]
  1. program cadena;
  2. var
  3.     Frase: shortstring ;
  4. begin
  5.  
  6.   WriteLn(high(Frase));
  7.   WriteLn(length(Frase));
  8.  
  9.  Readln()
  10. end.

Is it 0 like in fpc?

(This to come back to the initial question of the topic.)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #56 on: September 12, 2022, 05:20:11 pm »
Short string are not a managed type so they are not initialized individually per-variable, thus they would by default containg garbage, when are local inside function.

However global variables, as in your example, reside in program's global "data segment", which IS zeroed-out after allocation. Not per individual variable, but all the global variables at once with one huge FillChar( <first global variables>, <total SizeOf of all global variables and aligning>, #0)

A global short string varhence would become array[0..255] of #0
 Length(short string) is Ord(s[0]) and hence it would be zero. Actually all characters s[0 <= i <= 255] would be equal to #0.

The same would happen in namespace-contained global variables, like
Code: Pascal  [Select][+][-]
  1. type TX = class
  2.   public
  3.      class var s: string[20];
  4. end;
  5.  

This is also a global variable TX.s and it would be zeroed-out together with the rest of application's "data segment".

https://stackoverflow.com/a/132770/976391
Quote
Yes, this is the documented behaviour:
....
Global variables are always initialized to 0 etc as well;

Also here: https://stackoverflow.com/a/861178/976391
« Last Edit: September 12, 2022, 05:23:14 pm by Arioch »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #57 on: September 12, 2022, 05:22:50 pm »
string = UnicodeString
Yes i do agree, and just by now i found out that FPC also got it included  O:-)
Yes my bad, since i mainly work exclusive with and for Windows and their Api, i am used to automatical use wide variants of everything  :-*
(And excellent news, I can become Master-Guru es String without to buy+install Delphi.)
The community edition is free for everyone available, but has some minor limitations and is restricted to terms from embarcadero.
What is the result of length(s) with shortstring in Delphi.
Updated image:
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #58 on: September 12, 2022, 05:29:28 pm »
i mainly work exclusive with and for Windows and their Api

...i'd expect you then to exactly know.

Through my optics, it is Linux guy would would have no need to learn it and he would hardly to work with OLE/COM ever.

But Windows guy (at least me) when transiting (like my Delphi 5 -> Delphi XE2) would read "what's new" documents of all those versions and would see that D2009 introduced a nee string datatype, which is a big siren instantly. If they made new UnicodeString type - that should mean there is something really wrong with old WideString. What was that wrong, that precluded WideString name reuse??? At least that was how i learnt of the difference.

The community edition is free for everyone available, but has some minor limitations and is restricted to terms from embarcadero.

Worst of all, it does not have sources (or at least used to not have) and for me it means i can never rely on it.
When it would find some bug in the libraries (maybe tirggered by compiler bug, as was the case in Delphi 4.0) - i would not be able to research it, and would be able to neither patch the libraries nor to define and apply workaround.

Granted, to test which variables are initialized and which are not sources-less edition would suit. But for any serious work it would be poor choice.

Fred vS

  • Hero Member
  • *****
  • Posts: 3919
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #59 on: September 12, 2022, 05:32:26 pm »
What is the result of length(s) with shortstring in Delphi.
Updated image:

Supra-perfect!

Same result as Delphi, length = 0, problem really solved.  ;)

But I have to admit that, for a not Guru-es-String, it could be difficult, at first view, to understand the relationship between high(ashortstring) and length(ashortstring).

Many thanks KodeZwerg, it was a hard battle but we won.  ;D

Fre;D
« Last Edit: September 12, 2022, 05:42:46 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018