Recent

Author Topic: Set string by it's pointer raises error  (Read 1159 times)

Sanem

  • Full Member
  • ***
  • Posts: 173
Set string by it's pointer raises error
« on: November 24, 2020, 10:43:48 am »
Hi
I'm testing to change a string by its pointer, and the debugger raises an error for that, any opinion on why is this happening?
When I SetLength the string, it's working okay, like it's treating the var string as const in the first place.
Here are the code and sample project of the situation.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. var
  4.   S1, S2: String;
  5.   P: PChar;
  6. begin
  7.   SetLength(S1, 3);
  8.   P := Pointer(S1);
  9.   P^ := '1';
  10.  
  11.   S2 := 'ABC';
  12.   P := Pointer(S2);
  13.   P^ := '1';
  14.   ReadLn;
  15. end.
  16.  
« Last Edit: November 24, 2020, 10:54:07 am by Sonra »

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: Set string by it's pointer raises error
« Reply #1 on: November 24, 2020, 11:52:52 am »
S2 is given a literal value and the compiler stores literals as readonly. This may be the cause. Not sure.
BTW: Why do you use a raw pointer? PChar is already a pointer type, so why not use PChar
« Last Edit: November 24, 2020, 12:26:52 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Set string by it's pointer raises error
« Reply #2 on: November 24, 2020, 01:23:00 pm »
S2 is given a literal value and the compiler stores literals as readonly. This may be the cause. Not sure.

Yes, that is indeed the case (since 3.0.0). S2 points to readonly data. One needs to use UniqueString(S2) to get a real copy that one can modify.

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: Set string by it's pointer raises error
« Reply #3 on: November 24, 2020, 04:38:54 pm »
So:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. var
  3.   S1, S2: String;
  4.   P: PChar;
  5. begin
  6.   SetLength(S1, 3);
  7.   P := Pchar(S1);
  8.   P^ := '1';
  9.   S2 := 'ABC';
  10.   UniqueString(S2); // this solves your code
  11.   P := Pchar(S2);
  12.   P^ := '1';
  13. end.
Works also with raw pointer instead of Pchar but P is a typed pointer, i.e. Pchar.
« Last Edit: November 24, 2020, 04:41:41 pm by Thaddy »
Specialize a type, not a var.

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: Set string by it's pointer raises error
« Reply #4 on: November 25, 2020, 08:33:30 am »
S2 is given a literal value and the compiler stores literals as readonly. This may be the cause. Not sure.
BTW: Why do you use a raw pointer? PChar is already a pointer type, so why not use PChar

Thanks for your replay, I thought of this too, but I don't understand why?
I was testing, but this error is happening with PChar too.

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: Set string by it's pointer raises error
« Reply #5 on: November 25, 2020, 08:36:10 am »
S2 is given a literal value and the compiler stores literals as readonly. This may be the cause. Not sure.

Yes, that is indeed the case (since 3.0.0). S2 points to readonly data. One needs to use UniqueString(S2) to get a real copy that one can modify.


Thanks for the answer

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: Set string by it's pointer raises error
« Reply #6 on: November 25, 2020, 08:55:19 am »
I was testing, but this error is happening with PChar too.
You do not understand what we wrote. Look at my last example: call uniquestring().
My last example works with your code and just that extra line.
Specialize a type, not a var.

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: Set string by it's pointer raises error
« Reply #7 on: November 25, 2020, 09:52:24 am »
I was testing, but this error is happening with PChar too.
You do not understand what we wrote. Look at my last example: call uniquestring().
My last example works with your code and just that extra line.

That was the answer to "why I used Pointer instead of PChar".
But Yes, I understood, that's why I thanked you and did not ask any further questions. I even saw it in the wiki and completely understood the reason too.
Here:
https://wiki.freepascal.org/User_Changes_3.0#Literal_storage_memory_has_been_made_read-only




 

TinyPortal © 2005-2018