In article <45daf07b$0$15941$9b4e6d93@[EMAIL PROTECTED]
>,
Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@[EMAIL PROTECTED]
> wrote:
> Marco van de Voort schrieb:
> > I think the current system allows for easier optimization of non
mutated
> > by-value strings. But first, you will have to define "problem". I
don't see
> > the actual problem here?
> >
> > Why would you want caller pu****ng?
>
> Well, there is no problem. It was the first time I was looking at the
> internals in string parameters. I was just surprised to see how it
> actually works. My "problem" was that I didn't know WHY it is done this
> way. I was just guessing. Probably you're right, it could be for
> optimization and/or performance issues.
The reason is simply that this puts the string copying code once inside
the called procedure/function body, rather than everywhere the function
is called.
The copying of by-value strings is never optimized away to avoid code
like this giving unexpected results:
***
procedure test(s1: string; var s2: string);
begin
s2 := '';
if (s1 = '') then
writeln('problem');
end;
var
s: string;
begin
s := 'hello';
test(s,s);
end.
***
In principle, it could nevertheless be done if all of the following
conditions hold:
a) the procedure has no var/out string parameters
b) the procedure does not change any global string variables
c) the procedure does not write to any pointer location (or string
pointer location if you turn on strict aliasing).
In general, it's easier and less error prone to simply require the
programmer to use "const".
Jonas


|