[fpc-devel] Compiler hint for uninitialized local variable minor mistake

Jonas Maebe jonas.maebe at elis.ugent.be
Fri May 29 09:02:01 CEST 2009


On 29 May 2009, at 01:44, ABorka wrote:

> fillchar(tmp, sizeof(tmp), #0);
>
> gives the Hint: Local variable "tmp" does not seem to be initialized
> message upon compilation, which should not happen because it is  
> actually the initialization.

It is not possible to change this in the compiler. The reason is that  
if we change the first parameter of fillchar from a "var" parameter  
(as it is now) into an "out" parameter (so the hint would not be  
shown), then the behaviour of the code changes.

This is due to the fact that if you pass a variable to an out  
parameter and this variable is reference counted or contains reference  
counted elements (in case of an array/record/object), then the  
compiler has to insert finalization code at the caller side for this  
variable before passing it in. The result is that if tmp is reference  
counted but nevertheless somehow contained garbage, e.g. in a program  
like this:

type
   pr = ^tr;
   tr = record
     a: ansistring;
   end;

var
   p : pr;
begin
   p:=myspecialgetmem(sizeof(tr));
   fillchar(p^,sizeof(p^),0);
end;

then the program will try to finalize the garbage at run time and most  
likely crash. This may seem far fetched, but this sort of stuff  
happens in real code (I got crashes before I knew this and tried to  
change the move() procedure in this way).

Keep in mind that hints are the lowest level of "programming help"  
that the compiler has to offer, and it is generally impossible to make  
your code hint free. They are only intended to help you if you really  
are at wits end regarding what could be going wrong. Otherwise, it's  
best to stick to warnings and notes.


Jonas



More information about the fpc-devel mailing list