Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-07

can the compiler zero any out parameter of procedure/function automatically?

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dennis

I just found the cause of a strange bug is because I forgot to zero the out parameter of a procedure like.

procedure FindEvent (out TheEvent : TNotifyEvent);
begin
   TheEvent := nil;//I forgot to add this line before all the
processing, which sometimes did not assign to TheEvent at all
    .... long code...
end;
when later, I called.
var
   aEvent : TNotifyEvent;
      FindEvent(aEvent);
   if assigned(aEvent) then
    aEvent(nil);  -> cause serious error that crash the program.

I know it is my responsibility to initialize out parameters, but I think the compiler could help us by initializing all out parameters. Am I the only one with this view?

Dennis

Jonas Maebe
to Dennis
Dennis wrote
I know it is my responsibility to initialize out parameters, but I think the compiler could help us by initializing all out parameters.

The compiler will fill out-parameters with garbage if you use the -gt command line parameter, which can help you detect such problems more easily.

If you forget to assign a value to an out-parameter in a particular case, having it return nil by default could easily be just as wrong as it returning any other random value. For that reason, there is no support for automatically initialising out-parameters.

Jonas

Dennis Poon
to Jonas Maebe
Jonas Maebe wrote 11 lines
Dennis wrote: > I know it is my responsibility to initialize out parameters, but I think > the compiler could help us by initializing all out parameters. The compiler will fill out-parameters with garbage if you use the -gt command line parameter, which can help you detect such problems more easily. If you forget to assign a value to an out-parameter in a particular case, having it return nil by default could easily be just as wrong as it returning any other random value. For that reason, there is no support for automatically initialising out-parameters.

How about issue a compiler warning if one forgets to zero any out parameter?

Dennis

vfclists .
to Dennis Poon
Dennis Poon wrote 24 lines
Jonas Maebe wrote: > Dennis wrote: > >> I know it is my responsibility to initialize out parameters, but I think >> the compiler could help us by initializing all out parameters. >> > The compiler will fill out-parameters with garbage if you use the -gt > command line parameter, which can help you detect such problems more > easily. > > If you forget to assign a value to an out-parameter in a particular > case, having it return nil by default could easily be just as wrong as > it returning any other random value. For that reason, there is no > support for automatically initialising out-parameters. > > How about issue a compiler warning if one forgets to zero any out parameter? Dennis

The compiler might warn you about it, but it shouldn't initialize them out for you.

Computers/compilers derive their power from being idiot savants and it is wrong to make them anything other than that.

The compiler is supposed to implement what you say in the code, not what you mean. You are the one who has the responsibility of expressing the required states in as much detail as is possible.

Frank Church ======================= http://devblog.brahmancreations.com