Re: If eax = *p, then FreeMemory(*p) before ProcedureReturn?
Posted: Thu Dec 22, 2011 4:32 pm
@skywalk: I'm not sure what you are trying to accomplish with such a small static array with only 2 elements. This will surely cause you problems with nested calls.
Using the method I posted with an array of only 10 elements will allow calls like:
To preserve the result that is returned just copy the structure to a structured variable like so:
wilbert tried a variation of my approach but his required a global array of structured variables and also a search for unused variables each time the procedure was called. My approach simply used the next index.
I've used the approach that you demonstrated first where you passed the pointer in. This requires some acrobatic work to perform nested calls by using lots of single calls. The method that allocates a new structure each time requires you to free it when you're done with it. As you know that method doesn't work with nested calls without memory leaks.
Since your goal is to make nested calls and to keep the parameter count down I think the static array method would be a good choice. You only need to decide on how many indices to set aside to handle the depth of nesting that you expect. As you can see from my example above that can probably be kept under 10.
It would be great if this process was automated and made native. In the mean time it is not very difficult to implement it this way. If thread safety is needed it does get messier.
Using the method I posted with an array of only 10 elements will allow calls like:
Code: Select all
cSum2(cSum2(cSum2(cSum2(cSum2(cSum2(cSum2(cSum2(A,B),B),B),B)B),B),B),cSum2(A,cSum2(A,B)))
Code: Select all
;with variable
C.csi
CopyStructure(cSum2(A,B), C, csi)
;or with pointers
*D.csi
CopyStructure(cSum2(csum2(B,A), B), *D, csi)
I've used the approach that you demonstrated first where you passed the pointer in. This requires some acrobatic work to perform nested calls by using lots of single calls. The method that allocates a new structure each time requires you to free it when you're done with it. As you know that method doesn't work with nested calls without memory leaks.
Since your goal is to make nested calls and to keep the parameter count down I think the static array method would be a good choice. You only need to decide on how many indices to set aside to handle the depth of nesting that you expect. As you can see from my example above that can probably be kept under 10.
It would be great if this process was automated and made native. In the mean time it is not very difficult to implement it this way. If thread safety is needed it does get messier.