allocating memory in inner implementation

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

allocating memory in inner implementation

bader
       
in Math.jack class i have defined a static array which was initialized in the init function (using array.new function).

since Math.jack class does not contain a dispose function:
1) is it ok to leave this array undisposed?
2) or should i initialize this array in the function that uses it and dispose it there right before the return statement?

i think that the answer is 1, cuz otherwise there will be no need to the init() function of Math.jack at all.






a clarification would help,
bader.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: allocating memory in inner implementation

cadet1620
Administrator
Yes, you should allocate and initialize the static Array in Math.init().

Your point 2 brings up another consideration.  If you know that a commonly used function that needs a temporary Array will not be called reenterantly, it can be sped up by using a static Array that is allocated in init().  This saves calls to new() and dispose() every time the function is used.

--Mark

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: allocating memory in inner implementation

bader
thanks for the reply, i understand that i should allocate the array in the init() function. but where can i dispose it?!!
(i use the same array whenever the user call Math.multiply, but init() is called once so i cannot dispose it in any function other than init(), on the other hand i cannot dispose it in the init() )


did you mean that i can leave it undisposed?

thanks,
bader
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: allocating memory in inner implementation

cadet1620
Administrator
Correct.  You can't dispose memory that is allocated in init().

This isn't a problem since the various init() functions are only called once from Sys.init().  When you reset the computer and rerun the program Memory.init() [re]initializes the heap so the leftover allocated blocks are effectively disposed at that time.


If leaving these objects dangling bothers you, it's your OS!  Add
    function void final()
to your OS classes that have an init() that allocates memory.  Call all these final()s when Main.main() returns to Sys.init().

Thinking as I type...   Should the final()s should be called in Sys.halt()?  For Sys.halt() to be useful as a debugging abort like C/Java's 'assert' it shouldn't mess with the heap...  Maybe it would be better for halt() to call the free()s and add a Sys.abort() that doesn't call the free()s.

--Mark
Loading...