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

2009-05-29 Thread Jonas Maebe


On 29 May 2009, at 09:04, Micha Nelissen wrote:


Jonas Maebe wrote:
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.


Wasn't "outvar" a long wished feature to solve this?


Not that I know.


Jonas
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-05-29 Thread Jonas Maebe


On 29 May 2009, at 09:44, Flávio Etrusco wrote:


(...)

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:


Is Delphi's behavior the same?


Yes.


Doesn't this have some implications on COM calls? Or is it like this
way exactly to make COM work?


Indeed.

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.


And this make hints so much less useful...


That's the whole point of having a hints category: to put stuff in  
that we simply cannot guarantee to be useful (but which might be  
useful under some circumstances).



Jonas___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-05-29 Thread Flávio Etrusco
> (...)
>
> 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:

Is Delphi's behavior the same? I'm not 100% I've read it on any
official source, but I never saw it behave differently than "var
without the warning". Delphi didn't even have the decency to
force/warn one to initialize in the function :-/
Doesn't this have some implications on COM calls? Or is it like this
way exactly to make COM work?

>
> 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.
>

And this make hints so much less useful...

Best regards,
Flávio
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-05-29 Thread Micha Nelissen

Jonas Maebe wrote:
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.


Wasn't "outvar" a long wished feature to solve this?

Micha
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-05-29 Thread Jonas Maebe


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
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-05-28 Thread ABorka


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.


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel