Suggestions on array allocation pattern/rules of thumb?
In Lazarus I found (pseudo)some code like this:
v_Result := nil;
PrepareData(..., v_Results, v_ResultCount);
procedure PrepareData(..., var p_Results: PInteger; var p_ResultCount: Integer)
As you might guess my main gripe is about the repeated calls to
ReallocMem. The real case in the LCL is far from critical, it's
infrequently and the array won't ever be bigger than 10 elements, I
just thought it could a good example to hear from someone
knowledgeable in FPC heap-allocator about any "rules of thumb" :-)
Apart from making the parameters 'out' - ;) - what would you do?
1) This is this ok.
2) Allocate a buffer with maximum size and then trim it after the loop.
3) Declare a type for the maximum size, allocate it on the heap (in
4) Declare a type for the maximum size, allocate it on the stack (in
5) Any other options?
Any remarks regarding different scenarios is also appreciated.