Sign in Join

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

Mailing lists / fpc-pascal / 2010-03

Is it save to think default value of untouched string is ''?

9 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

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

lyh1

I have a a program that will read a string and parse the string to some sub-strings in sub routine.
I add every character to a local string buffer in sub routine. The code work well in delphi but suddenly fail in fpc.
When I watch the buffer, it display some garbage instead of null string when the buffer is untouched.
The following output is supposed to be 1(and it is true here), but in more complicated situation it is not true. You may get 'bla bla bla 1'.

procedure PrintString;
var str1 : string;
  procedure createContent(var buf : string);
   var str2 :string;
  begin
     buf := str2;
     buf := buf + '1';
  end;
begin
writeln(str1);
end;

My question is we should always assign null string to variable before use or this a bug if I get garbage in unused string?

Reply by email

Jonas Maebe
to lyh1
lyh1 wrote
My question is we should always assign null string to variable before use or this a bug if I get garbage in unused string?

If you want Delphi-compatible behaviour, you must add {$mode delphi} at the top of your program. In the default mode, "string" is an alias for shortstring instead of ansistring ({$h-} mode).

Jonas

Reply by email

Jonas Maebe
to Juha Manninen: And even then you should initialize strings. They are not quaranteed to be empty. Juha…
Juha Manninen wrote 6 lines
> If you want Delphi-compatible behaviour, you must add {$mode delphi} at the > top of your program. In the default mode, "string" is an alias for > shortstring instead of ansistring ({$h-} mode). And even then you should initialize strings. They are not quaranteed to be empty.

Reference-counted types sort of are guaranteed to be initialised on function entry (because otherwise the whole reference counting thing would fail 99% of the time).

Jonas

Reply by email

JoshyFun
to Juha Manninen

Hello FPC-Pascal,

you wrote
> If you want Delphi-compatible behaviour, you must add {$mode delphi} at the > top of your program. In the default mode, "string" is an alias for > shortstring instead of ansistring ({$h-} mode).

JM> And even then you should initialize strings. They are not quaranteed to be JM> empty.

Sorry ... What ? It must be garanteed in ansistring mode! or everything will simple kboom! There is no way to initialize a non initialized string.

Best regards, JoshyFun

Reply by email

Juha Manninen
to JoshyFun: JM> And even then you should initialize strings. They are not quaranteed to be JM> empty.…
Sorry ... What ? It must be garanteed in ansistring mode! or everything will simple kboom! There is no way to initialize a non initialized string.

By initializing I mean:

  str:='';

or some other assignment.
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.

Maybe it is not true with FPC.

Juha Manninen

Reply by email

Jonas Maebe
to Juha Manninen
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

Reply by email

Mattias Gaertner
to Juha Manninen: By initializing I mean: str:=''; or some other assignment. I remember having garbage in…
Show quoted text 12 lines
> Sorry ... What ? It must be garanteed in ansistring mode! or > everything will simple kboom! There is no way to initialize a non > initialized string. By initializing I mean: str:=''; or some other assignment. 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. Maybe it is not true with FPC.

SetLength does not initialize the string characters. ShortStrings are not always initialized.

Mattias

Reply by email