No, no.. In C++ I would not free anything because it's just a variable. There is nothing to free. I've created my own versions of TPA's and Dynamic arrays in C++ Here:
http://villavu.com/forum/showthread.php?t=82610In your function above, your asking it to push back 4000 points onto a NEW variable every time..
As long as my variable isn't allocated with New or my variable isn't a Box Type then I won't need to free anything. You don't delete structs/records/classes unless allocated on the stack using the new operator or malloc or anything of the sort in C++.. Fortunately for us, Simba does not create NEW structs every time.
Also programs do not free memory immediately and they certainly do not always free it after a function returns. In this case your function isn't returning, it keeps allocating more and more space every time long before it returns. It's basically like calling SetLength 100000 times in a row. For all you know, SetLength could easily be zeroing out all the memory allocated, nulling the pointer to it and allocating a new set.
When the operating system needs memory or has free time, it goes and clears unused memory and pointers for you, providing that you didn't crash it first. When your program closes then your OS will do a sweep of the memory it used. Sometimes it may do it while it's running but that depends on a lot of things.
Example would be to declare a local variable and return a reference to it. This is undefined behaviour because sometimes the memory isn't cleared so the value it returns may be correct. Other times, it's cleared as soon as the function returns and will give you random ascii characters or NULL values.
Simba is interpreted fully. It's going to behave far differently from running a DLL or a console application.
Pass By Value:
Create a Temporary Variable on the stack.
Function runs it's crap here and constantly pushes back values onto that TPA.
Return a COPY of the TPA and assign it to the calling variable.
Temporary Variable MAY or may NOT have been cleaned yet.
Everytime this function is ran, a new set of memory is allocated to not overwrite the previous if any.
Pass By Reference:
Functions runs its crap here and pushes back values onto a variable that's already created via a pointer to the variable's memory buffer.
No cleaning necessary, no copying, no anything.
In C++ vectors do the job just right. Know what they do? They allocate MORE than the amount of memory required so they don't have to keep allocating over and over and over everytime you push_back(Point(X, Y)). This is why vector::capacity() is different from vector::size()
Imagine calling malloc in C 5000 times in a row to push back 5k values. It doesn't make sense to do that if you already know your going to push 5k values flat.
The function you just posted took 8876 milliseconds to run just once using 1000x700 for the box.. LOL! That's a long ass time.
I know my post is long but just bare with me.. Try this function (it is a bit slower due to creating a new temporary variable but it will not lag you or leak I promise):
{$I SRL/SRL.SImba}
Function TPAFromBox2(const Box: TBox): TPointArray;
var
TPA: TPointArray; //Creates a temporary variable to hold our result.. This variable has the highest chance of being destroyed when the function returns.
x, y: integer;
l : integer;
begin;
SetLength(TPA, (Box.x2 - Box.x1 + 1) * (Box.y2 - Box.y1 + 1));
l:= 0;
For x := box.x1 to Box.x2 do
For y := box.y1 to box.y2 do
begin;
TPA[l].x := x;
TPA[l].y := y;
inc(l);
end;
Result:= TPA;
SetLength(TPA, 0); //Just a safety measure, we set it's length to 0 to null out it's memory pointer in hopes that it works.. and it indeed does.. sometimes it works without this, sometimes not.
//If your going to use points that are 1000x700 and stuff that large, KEEP SETLENGTH(0) or else your going to lag like shit!
end;
var
T, I: Integer;
TPA: TPointArray;
begin
I:= 0;
MarkTime(T);
Repeat
TPA:= TPAFromBox2(IntToBox(0, 0, 1000, 700));
Inc(I);
Until(I > 200);
writeln(TimeFromMark(T));
end.