On Sun, 19 Apr 1998 11:31:26 +0200 (MEST), Peter Gerwinski wrote:
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.
Why? ..because
For the sake of compatibility to BP, GPC initializes all global variables to zero/false/nil/empty string.
I've never seen a compiler predefine undefined variables anywhere either locally or globally before and I thought GPC would be consistant in this strangeness by predefining in both places. I didn't realize it was another Borland "feature". Obviously I assumed wrong.
But isn't this dangerous? It allows the compiler to happily accept code like: program test(output);
procedure test1;
var a, b : string (10);
begin a := a + b end.
begin test1; end.
And then core dump at runtime, or presumeably some other undefined result.
To me it make more sense to either set local variables to 'don't let the user shoot himself the foot' values (like global variables) or actual undefine them so that the compiler can warn/give errors about it.
That was twice the same program, and it produces 0 on my (Linux) system, too.
Ack, the second program should have been the same as above, except "a := a + b;" was replaced with "writeln(length(a));" But that is a moot point now. :)
Anyway, I thought this was the source of a runtime core-dump I was having with some TP source , obviously I was wrong and I'll see if I can better locate what is happening.
-Kevin
-- Kevin A. Foss --- kfoss@mint.net
From: "Kevin A. Foss" kfoss@mint.net Date: Sun, 19 Apr 98 12:24:47 -0400
On Sun, 19 Apr 1998 11:31:26 +0200 (MEST), Peter Gerwinski wrote:
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.
Why? ..because
For the sake of compatibility to BP, GPC initializes all global variables to zero/false/nil/empty string.
I've never seen a compiler predefine undefined variables anywhere either locally or globally before and I thought GPC would be consistant in this strangeness by predefining in both places. I didn't realize it was another Borland "feature". Obviously I assumed wrong.
But isn't this dangerous? It allows the compiler to happily accept code like:
program test(output);
procedure test1;
var a, b : string (10);
begin a := a + b end.
begin test1; end.
Not really because the programmer shouldn't make mistakes like this in the first place. From time to time these bugs can creep in and they can be hard to trace, but I don't think it is the job of the language frontend to support this feature. This is more the work of a bounds checker that should be integrated into the compiler backend and be mostly language independent. There has been a bounds checking library available for GCC 2.6.3 for a couple of years and there has also been talk of integrating it, or something like it, into EGCS.
To me it make more sense to either set local variables to 'don't let the user shoot himself the foot' values (like global variables) or actual undefine them so that the compiler can warn/give errors about it.
Does -Wuninitialized have the same effect ? You need to compile with optimisation turned on for this to work.
Cheers,
Nick B.