[fpc-devel] Dangerous optimization in CASE..OF

Jonas Maebe jonas at freepascal.org
Sat Jul 15 12:45:11 CEST 2017


On 14/07/17 20:21, Ondrej Pokorny wrote:
> In this case, please fix the compiler so that it doesn't generate 
> invalid values by default:
> 
> program Project1;
> 
> {$mode objfpc}{$H+}
> 
> type
>    TMyEnum = (one = 1, two);
> 
>    TMyClass = class
>    public
>      Enum: TMyEnum;
>    end;

Classes are explicitly documented to initialise their contents with 
zero, just like local variables are explicitly documented to be not 
initialised at all. The fact that the bitpattern for zero is a valid 
value for most types, does not change the fact that you remain 
responsible for intiailising data before use.

That is also why we warn when you use a local ansistring variable 
without initialising it first: even though it won't crash your program 
(due to underlying needs by the reference counting mechanism), it is 
still a logic error. Unfortunately, keeping track of the initialised 
status of fields of individual class instances is quite hard, so the 
compiler cannot warn you about such cases at this time.


Jonas



More information about the fpc-devel mailing list