Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Thats Insane - wrong memory setting by recursion??? (https://www.delphipraxis.net/67853-thats-insane-wrong-memory-setting-recursion.html)

JasonDX 20. Apr 2006 21:43

Re: Thats Insane - wrong memory setting by recursion???
 
Zitat:

Zitat von sk.Silvia
ok but ist the same also when i put

Delphi-Quellcode:
function TBinTree.FindMin(init:boolean):integer;
      begin
      if init then result:=self.Value;
      //ShowMessage('val '+ValToStr+' res'+inttostr(result));
      if value<result then
        begin
        result:=value;
        end;

      if not(left=nil) then result:=left.FindMin(false);
      if not(right=nil) then result:=right.FindMin(false);
      end;
value is always inicializes and result now too, but why the result changes besides on ShowMessage (again it gives the right value when i dont hide the showmessage dialog)

Yea, because at the first call result will be initialized. But every recursive call after that does not initialize the result variable. You call the method with false as init-parameter, which tells not to initialize the variable. So once again: result is undefined ;)

greetz
Mike

marabu 20. Apr 2006 21:53

Re: Thats Insane - wrong memory setting by recursion???
 
Hi Silvia,

you might want to try out this:

Delphi-Quellcode:
uses
  Math;

function TBinTree.FindMin: Integer;
var
  minLeft, minRight: Integer;
begin
  if Assigned(left)
    then minLeft := left.FindMin
    else minLeft := value;
  if Assigned(right)
    then minRight := right.FindMin
    else minRight := value;
  Result := MinIntValue(value, minLeft, minRight);
end;
Kind regards

marabu


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:58 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz