Type question
Blitz3D Forums/Blitz3D Beginners Area/Type question
| ||
;so i have a type: type Vector field x,y,z end type ;and some functions for addition and subtraction (just for addition for the question) function add.vector(lhs.vector,rhs.vector) local res.vector = new vector res\x = lhs\x + rhs\x res\y = lhs\y + rhs\y res\z = lhs\z + rhs\z return res end function ;so if i were to local v1.vector = new vector v1\x = 10 local v2.vector = new vector v2\y = 10 local v3.vector = new vector v3\z = 10 v3 = add(v3,add(v1,v2)) print v3\x + " " + v3\y + " " + v3\z ;prints out: 10 10 10 ;works fine right, but i think it creates a memory leak? |
| ||
Indeed it does. The result of adding v1 and v2 is currently "lost", as is the original value of v3. Still, although you don't get a garbage collector, Blitz3D does have the convenient "Delete Each vector" command: if you only ever use type "vector" for mathematical operations, you could always ignore memory leaks and call "Delete Each vector" every now and again in periods when you aren't currently using any vector objects for anything. So there's no true memory leak - you can fix it after the calculation is complete. It does require a clear separation between maths values and your other persistent objects though. On the other hand, I don't think there are really any other solutions that would let you write mathematical code in that expressive style. Depends which is more important to you really - clear maths or efficient program. Last edited 2011 |