Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic array

0 views
Skip to first unread message

Robert Ankka

unread,
Oct 24, 2001, 6:20:34 PM10/24/01
to
I had an array
Data : array[0...10,1...340] of string[3];
which was easily initialized as
for i:=0 to 10 do
for j:=0 to 340 do Data[i,j]:='';

Well, it is a Ham Radio log program, where the so-called DXCC-countries
are counted. There are 11 different 'bands', where the radio contact
takes place. There are some 333 countries actually, but very few have
collected so many. The array will be large, but in most cases quite
empty. To save memory a dynamic array is considered.

Procedure DxccCountries;
Var Data :array of array of string[3];
Band :integer; {bands 0..10}
Ctry :string[3]; {country number as string}
m,c :integer; {counters}
ok :boolean;

begin
initialize(data);
SetLength(Data,11); { [Data[0..10, ok }
c:=0;
While not (DbTable.EOF) do begin
Band:=DbTable.Fields[2].AsInteger; {a band, between 0..10}
Ctry:=DbTable.Fields[3].AsString;
ok:=true;
If Ctry<>'' then begin {sometimes Ctry not
saved}
SetLength(Data[2],c+1);
For m:=0 to c do begin
if Data[Band,m] = Ctry then ok:=false; {Ctry already
counted}
end;
If ok =true then begin
Data[Band,c]:=Ctry; {this is a new Ctry on this Band}
inc(c);
end;
DbTable.Next;
....
Finalize(Data);

It seems to function except that sometimes an array value is not empty
nor a string[3], but its length is 251 and it contains rubbish, because
the array is not initialized(?). I tried to use the Country number also
as an integer but to no avail.

I could not create the SetLength(Data,i,j) -rectangular method to
function correctly. Another point is, do we save much memory in using
the dynamic array?
-Rob


M.H. Avegaart

unread,
Oct 25, 2001, 10:03:29 AM10/25/01
to
You are using Initialize and Finalize wrong.
1. You don't need to call Initialize for short strings (only long strings,
variants and interfaces).
2. You should call Initialize after you allocated memory (so AFTER the
SetLength).


"Robert Ankka" <robert...@hot---mail.com.invalid> wrote in message
news:9r7eqt$4b5$1...@nyytiset.pp.htv.fi...

Robert Ankka

unread,
Oct 29, 2001, 4:15:36 AM10/29/01
to

"M.H. Avegaart" <aveg...@NOSPAMmccomm.nl> wrote in message
news:9r964c$c8k$1...@scavenger.euro.net...

> You are using Initialize and Finalize wrong.
> 1. You don't need to call Initialize for short strings (only long
strings,
> variants and interfaces).
> 2. You should call Initialize after you allocated memory (so AFTER the
> SetLength).

As in this program it is question of short strings, no initialization is
needed? But how to prevent the array variable containing some 351
characters of rubbish?

How should the Finalize be used in the example I gave?
-Rob

Robert Ankka

unread,
Oct 30, 2001, 2:07:18 AM10/30/01
to
Here we have
var
data1, data2 :array of array of string[3];

The dispose example is:
type
Str3 = string[3];
var
P : ^Str3;
begin
New(P);
....
Dispose(P);

How to use it to free the memory of data1, data2?
As I could not do it I used instead
SetLength(data1,0,0);
SetLength(data2,0,0);
at the end of the program, which seems to empty the huge arrays nicely.
-Rob

Maarten Wiltink

unread,
Oct 30, 2001, 4:23:35 AM10/30/01
to
Robert Ankka wrote in message <9rljj9$4kp$1...@nyytiset.pp.htv.fi>...


At the end of your program, all memory will be freed by
the OS anyway.

Those arrays are, for lack of a better word, static.
The variables referencing them aren't pointers and
you shouldn't free the memory from under them. If
you want dynamic memory allocation, that's fine -
but don't start by putting variables on the stack.

Groetjes,
Maarten Wiltink

0 new messages