return once or multiple times ?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

return once or multiple times ?

Lozminda
I have this kind of stuff written in jack

      if (BlockNum > other.getBlockNum() )
      {
         if (BothNeg) {return false;}
         else {return true;}
      }

      if (BlockNum < other.getBlockNum() )
      {
         if (BothNeg) {return true;}
         else {return false;}
      }

as part of a larger method, I can't find in the book that you should only have one return call but i'm guessing as i've just got got a memory overflow that the answer is yes, return once not here there and everywhere like the C family.
If someone wiser than I could confirm or deny that'd be great. Am hoping (but not expecting) for the latter so I don't have to rewrite !

Cheers in advance, happy new year to all !
Lozminda
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

Lozminda
Ps The memory overflow could be an array issue too, if anyone can point me in the right direction re return it'd save me some work.

Pps have just read chapter 9 again, no mention of multiple returns being bad, and as the only issue in the VM code is thinking about the return calling point....It's a while since I did chapter 8 (non programming work), any thoughts ?

PPps Haven't been able to find anything else on the web, I am disposing() arrays and strings when done with.. Ta !
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

ivant
I don't think there is anything wrong with multiple returns. You can try it, by rewriting your program with single return and test again.

Memory problems are some of the hardest to diagnose. In C/C++ there are tools like Valgrind, which instrument your code so it's easier to locate such problems and even they aren't 100% reliable in catching things. You can read this thread for some discussions regarding similar problems. This thread also mentions an alternative implementation of the Memory class, which contains additional functions, like Memory.debugDumpHeap() and Memory.debugHeapStats(). I believe it's from Mark (cadet1620).

You can also post the code somewhere or send it to me to check.
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

WBahn
Administrator
In reply to this post by Lozminda
Multiple returns are allowed. If you are not implementing the return as a subroutine call, then multiple returns will chew up some ROM memory, but as long as you don't run out you should be fine. This is largely a nonissue if you are using the VM emulator as I don't think it has a meaningful limit on program size.

Your problem is likely something else involving improper memory allocation or deallocation. Are using using the authors' OS files?
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

Lozminda
Great news re multiple returns.

Yes I am using authors OS files. I get the error after about 10 loops of my program. Am gonna do a bit more investigation and see if I can come up with some useful info.

Thanks very much !

Lozminda
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

Lozminda
In reply to this post by ivant
Ps Have been coding in C recently (in fact i wrote the VM translator in C) so have some experience with memory leaks, however as you say there are tools to help a programmer. I did toy with the idea of writing a gdb for jack programs, but So don't have the time ! (unfortunately)

Cheers
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

Lozminda
In reply to this post by WBahn
I've sorted one bug, no change

The error Seems to becoming from (for example):      

 do Output.printString("Pointer is:    ");

I'm doing a lot of printing out variables as i test bits of program within a loop. The call stack (when i get the error) is

NumBer.Print
String.new
Memory.alloc

NumBer.Print only contains commands such as:
 
do Output.printString("NumOfDcmls is:    ");
         -"-           -"-      ("Pointer is:");
         -"-           -"-       "BlockNum is:    "

Could it be an issue with printString, or my use of it ?
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

WBahn
Administrator
When you use a string literal, are you generating code to free the literal after use?
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

Lozminda
In reply to this post by Lozminda
Sorry for multiple posts, am doing this as I conduct my research.

It does indeed appear to be printString().
I've removed 6 of the printString()s leaving one so i could keep track of an important variable, and the program crashes after 250 loops, rather than the 38 that was before (It was consistently 38 what ever else I varied)

(45 min or so later....)

It really does appear to be printString().
The solution is any string to be printed eg "Hello World, crikey i love strings in jack almost as much fun as C", it would seem best to declare that as a String type, then printString(String_variable) and then before returning from the function or whatever disposing() of that string (do String_variable.dispose().

I have done that and my program does now work without crashing (250 loops printing six lines of text every time). I vaguely remember reading that somewhere, but couldn't find it either in the book or online subsequently.

In summary if you're using printString() a lot declare Strings, printString() the string and then dispose()
Reply | Threaded
Open this post in threaded view
|

Re: return once or multiple times ?

Lozminda
In reply to this post by WBahn
I am now, problem solved it would seem. Weird thing was I couldn't find any info on this anywhere, even tho I have it as a vague recollection. The info ocean isn't static !

Cheers