AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Thats Insane - wrong memory setting by recursion???
Thema durchsuchen
Ansicht
Themen-Optionen

Thats Insane - wrong memory setting by recursion???

Ein Thema von sk.Silvia · begonnen am 20. Apr 2006 · letzter Beitrag vom 20. Apr 2006
Antwort Antwort
Seite 1 von 2  1 2      
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#1

Thats Insane - wrong memory setting by recursion???

  Alt 20. Apr 2006, 20:09
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??
  Mit Zitat antworten Zitat
Dust Signs

Registriert seit: 28. Dez 2004
Ort: Salzburg
379 Beiträge
 
#2

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

  Alt 20. Apr 2006, 20:15
Line 17: there is an "exit" missing -_-

Dust Signs
(aka AXMD in der EE)
Die Nummer, die Sie gewählt haben, ist imaginär. Bitte drehen Sie Ihr Telefon um 90° und versuchen Sie es erneut.
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#3

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

  Alt 20. Apr 2006, 20:17
iam sorry iam not getting it " Line 17: there is an "exit" missing -_- " Can you explain?
  Mit Zitat antworten Zitat
Dust Signs

Registriert seit: 28. Dez 2004
Ort: Salzburg
379 Beiträge
 
#4

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

  Alt 20. Apr 2006, 20:23
//EDIT: Sorry, I was wrong.

Dust Signs
(aka AXMD in der EE)
Die Nummer, die Sie gewählt haben, ist imaginär. Bitte drehen Sie Ihr Telefon um 90° und versuchen Sie es erneut.
  Mit Zitat antworten Zitat
Benutzerbild von JasonDX
JasonDX
(CodeLib-Manager)

Registriert seit: 5. Aug 2004
Ort: München
1.062 Beiträge
 
#5

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

  Alt 20. Apr 2006, 20:26
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
Mike
Passion is no replacement for reason
  Mit Zitat antworten Zitat
xaromz

Registriert seit: 18. Mär 2005
1.682 Beiträge
 
Delphi 2006 Enterprise
 
#6

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

  Alt 20. Apr 2006, 20:36
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
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#7

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

  Alt 20. Apr 2006, 21:05
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
  Mit Zitat antworten Zitat
Dax
(Gast)

n/a Beiträge
 
#8

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

  Alt 20. Apr 2006, 21:09
You're not initializing the local variables at the very beginning of your method..
  Mit Zitat antworten Zitat
xaromz

Registriert seit: 18. Mär 2005
1.682 Beiträge
 
Delphi 2006 Enterprise
 
#9

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

  Alt 20. Apr 2006, 21:11
Hello,
Zitat von sk.Silvia:
yes your code is great xaromz, thats what i wanted
Thanks .
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
 if value<result then Here Result wasn't initialized. Your initializeation followed after that comparison.

Greets
xaromz
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#10

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

  Alt 20. Apr 2006, 21:38
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)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:37 Uhr.
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