Sign in Join

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

Mailing lists / fpc-pascal / 2004-07

force ansistrings to be copied on duplication

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Marc Santhoff

Hi,

according to the manual AnsiStrings are 'copied on change'.

Can the compiler be forced to copy on duplication?

--- like this ---
var
  s1:PChar='something';
  s2:PChar;
begin
  s2 := s1;
  if (s1  s2) then
    writeln('Okay')
  else
    writeln(':-( only a doubled pointer');
end;

---

As an alternative I think I would have to use StrNew/StrDispose and do some pointer collection. That would be too much effort in my case...

TIA,
Marc

Reply by email

Marc Santhoff
to Marc Santhoff

Hi again,

--- like this --- var s1:PChar='something';

this should be

    s1:AnsiString='something';

of course ...

Show quoted text 12 lines
s2:PChar; begin s2 := s1; if (s1 s2) then writeln('Okay') else writeln(':-( only a doubled pointer'); end; ---

Sorry,
Marc

Reply by email

Florian Klaempfl
to Marc Santhoff
Marc Santhoff wrote 15 lines
Hi again, Am Do, den 29.07.2004 schrieb Marc Santhoff um 06:33: >--- like this --- >var > s1:PChar='something'; this should be s1:AnsiString='something'; of course ...

Use UniqueString(s2) to get a real copy.

Show quoted text 25 lines
> s2:PChar; > >begin > s2 := s1; > if (s1 s2) then > writeln('Okay') > else > writeln(':-( only a doubled pointer'); > >end; > >--- Sorry, Marc _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Marc Santhoff: according to the manual AnsiStrings are 'copied on change'. Can the compiler be forced to…
Marc Santhoff wrote
according to the manual AnsiStrings are 'copied on change'.

Correct.

Can the compiler be forced to copy on duplication?

No, but the RTL can be:

Procedure UniqueString(Var S : AnsiString);

This procedure will make variable S point to an ansistring with the same contents as the current one, but with reference count 1 (so it will generate a copy if necessary).

Jonas

Reply by email

Marc Santhoff
to Jonas Maebe
Show quoted text 15 lines
On 29 jul 2004, at 06:33, Marc Santhoff wrote: > according to the manual AnsiStrings are 'copied on change'. Correct. > Can the compiler be forced to copy on duplication? No, but the RTL can be: Procedure UniqueString(Var S : AnsiString); This procedure will make variable S point to an ansistring with the same contents as the current one, but with reference count 1 (so it will generate a copy if necessary).

Ah, yes. Very fine, so I can decide for each string, right what I needed.

Thank you both,
Marc

Reply by email