Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer problem

Status
Not open for further replies.

aliencreeper

Programmer
May 5, 2004
4
SK
Greetings!

I made gomoku (noughts - and crosses) program using minimax and alpha-beta pruning to seek best moves. The play board is defined as 2D array [1..15, 1..15] of shortint.
I made lot of auxiliary functions that manage this board, find rows, wins, marks and so on. During computer's move, they are used around 800 000 times. One of their parameter is this play board. I decided not to use new board variable but pointer to this board as a parameter to save memory. It is absolutely functional however memory usage is rapidly raising during recursive search for moves. (after 5 moves, there is around 70 MB of RAM used only by my program!) ... please help me to solve this problem, I'll be very grateful.

P.S.: After search, memory usage won't drain.
 
Hi aliencreeper,

you'll have to show us some actual code here. If I understand correctly, each move is a new board?

--------------------------------------
What You See Is What You Get
 
I removed it. Memory usage is now far more smaller but still increases very quickly (= 600 KB / second).

Maybe, problem is also here:
---------------------------------------------------------------------------------
TYPE
p_array = ^t_array;
t_array =
record
....
next_cell: p_array;
end;

VAR A: p_array;
---------------------------------------------------------------------------------
I don't know exactly how many cells is neccessary every move, so I use this dynamic array in many functions and procedures.

Is it possible, that space allocated by this array remains allocated after termination of function, which holds it?
I attemted FreeMem and Dispose to release it, but it did nothing.
 
what you are showing here is not a (delphi) dynamic array, but a linked record list via pointers. where in your code are you requesting memory space (eg New() or getmem() )?

maybe you can change your code like this :

type your_record = record;
...
end;

type Tdyn_array = array of your_record;

var dyn_array : TDynarray;

...
assign memory space for new element

setlength(dynarray, 10); //reserve 10 elements
...
setlength(dynarray,0);// free memory space



--------------------------------------
What You See Is What You Get
 
How exactly your t_array record is?

buho (A).

 
I done it already. Those lists held memory allocated even after procedure / function termination. I've never worked with these lists before, so I didn't know, how to dispose them.

I think a Delphi dynamic array is a bit ineffective in this case ...
The number of cells is increasing during for ... to ...
Using it would make me use SetLength too often.

Thank you!
 
If you are in a "for... to..." you have not problem at all, as PASCAL "for" upper limit need to be known when the iteration starts (PASCAL is not "C", you can't change the limit or have a complex condition). So you can dimensionate the array up front.

If you are using "break" or other non-PASCAL tricks to prematurely end the iteration work, better use "while" or "repeat" (please, lets keep PASCAL as PASCAL is :).

You have a clean solution, anyway: grow the array in chunks of 10 or 20 or 50 items (whatever is best for you) and keep a "MaxIndex" var indicating which is the last really loaded item, or use a "sentinel value" to mark the first unused one.

Furthermore, any programmer allocated pointer needs to be freed by the programer (interfaces, AnsiStrings and dyn arrays are "smart pointers" but they are not strictly "programmer allocated pointers").

Thanks God PASCAL still have not garbage collectors messing the flow of my code. Well... regretabily things will change in Delphi.NET :(

buho (A).

PS: I'm pretty much PASCALish today... it is a side effect of too much ASP programming yesterday.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top