Hello, Today I was playing around with some code and was getting some pretty wacky results from a couple string functions (anything from random trash to SIGSEGV.) It appears that for some reason strings that are defined in procedures/functions are not initialized to a length of 0.
For example: program test(output);
var a : string (10); begin writeln(length(a)) end.
On both Linux (gpc971001) and OS/2 (gpc980410) I get a result of 0, no problem.
However: program test(output);
var a : string (10);
begin writeln(length(a)); end.
Produces 65536 in OS/2 and 134515920 in Linux. Interestingly, if you declare more strings, usually by the third or fourth string the length will actually be 0 -- but I've seen gpc on Linux return negative integers too!
I'm not sure how much of a bug this is as I'm not sure that the user should assume *anything* about undefined variables. I haven't checked the EP standard about this yet. It probably should be set to 0 for BP compatibility as the code I was trying to port to GPC was written for TP and assumed undefined strings would have a 0 length.
-Kevin -- Kevin A. Foss --- kfoss@mint.net
According to Kevin A. Foss:
[...] It appears that for some reason strings that are defined in procedures/functions are not initialized to a length of 0.
Why should they? Local variables have no predifined value on any Pascal compiler I know.
For example: program test(output);
var a : string (10); begin writeln(length(a)) end.
On both Linux (gpc971001) and OS/2 (gpc980410) I get a result of 0, no problem.
However: program test(output);
var a : string (10);
begin writeln(length(a)); end.
Produces 65536 in OS/2 and 134515920 in Linux.
That was twice the same program, and it produces 0 on my (Linux) system, too. For the sake of compatibility to BP, GPC initializes all global variables to zero/false/nil/empty string.
I'm not sure how much of a bug this is as I'm not sure that the user should assume *anything* about undefined variables.
He shouldn't. Neither in GPC nor in BP which also does not set local strings to length zero (I just checked with BP7).
Greetings,
Peter