Delphi-PRAXiS
Seite 1 von 2  1 2      

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)

sk.Silvia 20. Apr 2006 20:09


Thats Insane - wrong memory setting by recursion???
 
thats insane, try to run this...you will get the most highest parent (node 0) as result, but try to run it with ShowMessage('val '+ValToStr+' res'+inttostr(result)); and you will get the right result...also you get the right result by adding result:=result;
but WHY?????????????????????????????????????

OMG WHY???????????????????

Delphi-Quellcode:
type TBinTree=class
      Value:integer;
      Left,Right: TBinTree;
      ...
      function FindMin(init:boolean):integer;
      end;
 

 function TBinTree.FindMin(init:boolean):integer;
      begin
      //ShowMessage('val '+ValToStr+' res'+inttostr(result));
      //result:=result;
      if value<result then
        begin
        result:=value;
        end;
      if init then result:=self.Value;
     
      if not(left=nil) then result:=left.FindMin(false);
      if not(right=nil) then result:=right.FindMin(false);
      end;

in prog -> ShowMessage(IntToStr(BinTree.MindMin(true)));


HOW CAN SHOWMESSAGE CHANGE THE RESULT OF A RECURSION????? AND WHY HAVE RESULT:=RESULT; THE SAME EFFECT??

Dust Signs 20. Apr 2006 20:15

Re: Thats Insane - wrong memory setting by recursion???
 
Line 17: there is an "exit" missing -_-

Dust Signs

sk.Silvia 20. Apr 2006 20:17

Re: Thats Insane - wrong memory setting by recursion???
 
iam sorry iam not getting it " Line 17: there is an "exit" missing -_- " Can you explain?

Dust Signs 20. Apr 2006 20:23

Re: Thats Insane - wrong memory setting by recursion???
 
//EDIT: Sorry, I was wrong.

Dust Signs

JasonDX 20. Apr 2006 20:26

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

Zitat von sk.Silvia
Delphi-Quellcode:
 function TBinTree.FindMin(init:boolean):integer;
      begin
      //ShowMessage('val '+ValToStr+' res'+inttostr(result));
      //result:=result;
      if value<result then
        begin
        result:=value;
        end;
      if init then result:=self.Value;
     
      if not(left=nil) then result:=left.FindMin(false);
      if not(right=nil) then result:=right.FindMin(false);
      end;

in prog -> ShowMessage(IntToStr(BinTree.MindMin(true)));
HOW CAN SHOWMESSAGE CHANGE THE RESULT OF A RECURSION????? AND WHY HAVE RESULT:=RESULT; THE SAME EFFECT??

First rule of local variables: Do always initialize them before usage ;) You forgot that, and that might be the reason for the strange behaviour.

greetz
Mike

xaromz 20. Apr 2006 20:36

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

one problem (but not the only one I guess) with your code is that you haven't set the Result prior to comparing it to "Value". So "Result" is undefined.
I think you were trying to do something like this:
Delphi-Quellcode:
function TBinTree.FindMin: Integer;
begin
  Result := Value; // Assume current Value is lowest

  if Assigned(Left) then
    Result := Min(Left.FindMin, Result); // Get lowest value [current, left]

  if Assigned(Right) then
    Result := Min(Right.FindMin, Result); // Get lowest value [current, right]
end;
xaromz

sk.Silvia 20. Apr 2006 21:05

Re: Thats Insane - wrong memory setting by recursion???
 
yes your code is great xaromz, thats what i wanted

but i initialized the value of result, thnaks the init boolean (by the first run, the value of result was set to current), so it wasnt undefined, iam not getting it :(

Dax 20. Apr 2006 21:09

Re: Thats Insane - wrong memory setting by recursion???
 
You're not initializing the local variables at the very beginning of your method.. ;)

xaromz 20. Apr 2006 21:11

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

Zitat von sk.Silvia
yes your code is great xaromz, thats what i wanted

Thanks :) .
Zitat:

Zitat von sk.Silvia
but i initialized the value of result, thnaks the init boolean (by the first run, the value of result was set to current), so it wasnt undefined, iam not getting it :(

Your first piece of code was
Delphi-Quellcode:
 if value<result then
Here Result wasn't initialized. Your initializeation followed after that comparison.

Greets
xaromz

sk.Silvia 20. Apr 2006 21:38

Re: Thats Insane - wrong memory setting by recursion???
 
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)


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:30 Uhr.
Seite 1 von 2  1 2      

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