Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
security rule #1 : never assume anything... 2010/3/7 lyh1 lyh1...@hotmail.com: But ShortStrings also store the length in string[0]. If I do a append, I think the string will append to string[1] So don't assume string are always initialized with null string? Message: 5 Date: Sun, 7 Mar 2010 00:17:03 +0100 From: Mattias Gaertner nc-gaert...@netcologne.de Subject: Re: [fpc-pascal] Is it save to think default value of untouched string is ''? To: fpc-pascal@lists.freepascal.org Message-ID: 20100307001703.2a1b2...@limapholos Content-Type: text/plain; charset=US-ASCII SetLength does not initialize the string characters. ShortStrings are not always initialized. Mattias ___ fpc-pascal maillist - fpc-pas...@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
But ShortStrings also store the length in string[0]. If I do a append, I think the string will append to string[1] So don't assume string are always initialized with null string? Message: 5 Date: Sun, 7 Mar 2010 00:17:03 +0100 From: Mattias Gaertner nc-gaert...@netcologne.de Subject: Re: [fpc-pascal] Is it save to think default value of untouched string is ''? To: fpc-pascal@lists.freepascal.org Message-ID: 20100307001703.2a1b2...@limapholos Content-Type: text/plain; charset=US-ASCII SetLength does not initialize the string characters. ShortStrings are not always initialized. Mattias ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Is it save to think default value of untouched string is ''?
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? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
On 06 Mar 2010, at 21:13, 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___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
On 06 Mar 2010, at 22:03, Juha Manninen 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). 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___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re[2]: [fpc-pascal] Is it save to think default value of untouched string is ''?
Hello FPC-Pascal, Saturday, March 6, 2010, 10:03:49 PM, 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
I thought only global variables were initialized to zero and local ones were just cereated on the stack? Bart ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
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___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Is it save to think default value of untouched string is ''?
On Sun, 7 Mar 2010 01:13:05 +0200 Juha Manninen juha.manni...@phnet.fi wrote: 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal