Forum > General

Some incompatibilities with Turbo Pascal regarding string handling

(1/15) > >>

Avinash:
Well, there is such a type as a pascal string.

And there are procedures that have pascal strings as parameters.

But there are also procedures that require a pointer to a pascal string (PString).

And here difficulties arise when it is necessary to pass the result of a string expression or function to such procedures.
You have to define an extra variable:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var S2: String;...S2 := FExpand(S);XProc(@S2);
However, there is a trick to make things easier:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function StrPtr(const S: String): PString;begin  StrPtr := @S;end;
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---XProc(StrPtr(FExpand(S)));In this case, the compiler reserves a hidden string variable in the stack, and the pointer produced by StrPtr is valid until the procedure in which we do this is completed.
This simplifies the source code and makes the binaries a bit smaller.

But let's ask a question, what will happen if there are several StrPtr calls in one procedure? Will multiple hidden variables be reserved, or will the same one be used?

Let's check:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  PString = ^String;   function StrPtr(const S: String): PString;  begin    StrPtr := @S;  end;   procedure Test(PS1, PS2, PS3, PS4: PString);  begin    WriteLn(PS1^);    WriteLn(PS2^);    WriteLn(PS3^);    WriteLn(PS4^);  end; var  S1, S2, S3, S4, S5, S6, S7, S8: String; begin  S1 := '1111';  S2 := '2222';  S3 := '3333';  S4 := '4444';  S5 := '5555';  S6 := '6666';  S7 := '7777';  S8 := '8888';   Test(StrPtr(S1+S2), StrPtr(S3+S4), StrPtr(S5+S6), StrPtr(S7+S8));end.
Turbo Pacal by Borland is unbreakable:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---11112222333344445555666677778888
Free Pascal:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---11112222111122221111222211112222(or 77778888 depending on calling convention)

PascalDragon:
You are relying on implementation details here. It is nowhere documented that temporary variables (in this case the result of the string concatenations) can not be reused right away which is what FPC is doing and it is perfectly in its right to do so.

440bx:

--- Quote from: PascalDragon on August 25, 2022, 08:57:13 am ---You are relying on implementation details here. It is nowhere documented that temporary variables (in this case the result of the string concatenations) can not be reused right away which is what FPC is doing and it is perfectly in its right to do so.

--- End quote ---
That doesn't sound right.  As a matter of course, the compiler should treat every parameter as being totally independent of any other parameter and, for this reason, it should be creating a temporary for each one.

BobDog:
In the function StrPtr, I cannot assign a new value (inside the function) for the string parameter S, so I assume that S is actually a proper constant.
(I get:Error: Can't assign values to const variable)
So in light of that, supply that function with an actual constant.
concatenation of two constants = another constant.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  PString = ^String;   function StrPtr(const S: String): PString;  begin    StrPtr := @s;  end;   procedure Test(PS1, PS2, PS3, PS4: PString);  begin    WriteLn(PS1^);    WriteLn(PS2^);    WriteLn(PS3^);    WriteLn(PS4^);  end; const  S1 = '1111';  S2 = '2222';  S3 = '3333';  S4 = '4444';  S5 = '5555';  S6 = '6666';  S7 = '7777';  S8 = '8888'; begin  Test(StrPtr(S1+S2), StrPtr(S3+S4), StrPtr(S5+S6), StrPtr(S7+S8));readln;end.

440bx:

--- Quote from: BobDog on August 25, 2022, 06:25:43 pm ---In the function StrPtr, I cannot assign a new value (inside the function) for the string parameter S, so I assume that S is actually a proper constant.
(I get:Error: Can't assign values to const variable)
So in light of that, supply that function with an actual constant.
concatenation of two constants = another constant.

--- End quote ---
The "const" in StrPtr tells the compiler that the parameter cannot be changed by the function, it doesn't mean the parameter must be a constant. As long as the parameter is a "String" the parameter's type requirements are met.

Also, the concatenation of two string constants cannot be a constant. The resulting concatenation has to be placed in read/write memory otherwise it is not possible to create the concatenation.

Navigation

[0] Message Index

[#] Next page

Go to full version