[fpc-pascal] Is it save to think default value of untouched string is ''?

Jonas Maebe jonas.maebe at elis.ugent.be
Sun Mar 7 00:15:50 CET 2010


On 07 Mar 2010, at 00:13, Juha Manninen wrote:

> I remember having garbage in strings in Delphi when I assumed they are empty.
> Or was it only with function's string return value, I am not sure.
> Anyway, after that I was careful to always assign my strings.

You're right that it can be different in case of function results. In case the function result is passed to the function via a hidden call-by-reference parameter, then if you write "x:=func(...)", the "result" variable inside func may directly refer to "x" rather than to some temporary value. And I think that when inlining, the compiler may also reuse previously used ansistring temps for local variables of inlined functions (which still may contain a value from evaluating a previous expression).

So even with reference-counted types, it's indeed best to always explicitly assign a value if you want to be certain that they contain said value.


Jonas


More information about the fpc-pascal mailing list