O/H Saurabh Rai έγραψε:
>
> Type
> PRec = ^Rec;
> Rec = packed record
> VarP : integer ;
> End;
>
> Type
> RecArray = array of PRec ;
>
> Assigninment :
>
> function TCLassOne.FillArray ;
> Var
> VarOne :PRec ;
> VarI : Integer l
> Begin
> SetLength(RecArray,500);
> for VarI=0 to 500 do
i think you pass the end of array by one here
that is you read invalid memory.
I usually write
for i := Low(RecArray) to High(RecArray) do
> Begin
> New(VarOne );
> VarOne^.VarP := VarI;
> RecArray[VarI]:= VarOne ;
> End;
> End;
>
> Releasing Mem :
>
> function TCLassOne.ReleaseArray;
> Var
> VarI : Integer l
> Begin
> for VarI := 0 to Length(RecArray) - 1 do
> begin
> if (RecArray[VarI ] = NIL) then break;
> Dispose(RecArray[VarI ]);
> RecArray[VarI ] := NIL;
> end;
> End;
>
>
> Hi seniours ,
> im silent user of this group , really scared for asking my first
> question .but i want expert review ..
We are here to help eachother.
So don't be scared.
This world belongs to brave hearts.
> i ve mentioned two functions above first for assignment of array and second
> for reasing mem . but i ve some problem coz mem is not getting free untill
> appication is closed . means fucntion ReleaseArray isnt releasing mem of
> records held by array of pointers .
> pls pls im stucked .
> hope u understand my prob as i dun 've gud english .
> thanks and regards .
> Saurabh rai.
If your array is a global variable
or belongs to an object that is going to live
as long your application stays alive
then, yes, that memory remains occupied
untill the termination of your program.
But why this is a problem?
Unless the amount of memory your array needs is huge
I see no problem at all.
And what is huge nowadays? 1, 2, 10 MBytes? More?
BTW, the rules regarding memory and dynamic arrays
are the same as for strings.
That is both dynamic arrays and strings are
reference counted.
When the reference reaches zero the memory is deallocated.
Local variables goes out of scope when the function terminates.
That means that
procedure F;
var
S: string;
begin;
S := 'a very long string here';
blah
blah
end; // << the memory the S points to is deallocated here...
...unless, inside that routine, you assign S
to a variable with more broad scope.
The same stands true for dynamic arrays.
Also you may finalize (read dispose) a dynamic array
by assigning nil to it.
MyDynamicArrayVariable := nil;
Provided that the elements of your array do not need
explicit deallocation.
And that brings me to a question: Do you really need
an array of Pointers to a record?
Also, I avoid using New() and Dispose().
I prefer GetMem(), AllocMem() and FreeMem()
...although I don't remember why (lol)
Here is a variation of your code
type
PRec = ^TRec;
TRec = packed record
Value : Integer;
end;
type
TRecArray = array of TRec; // type for a dynamic array of TRec
PRecArray = array of PRec; // type for a dynamic array of PRec
//both must be in global scope somewhere
var
arRec : TRecArray = nil;
parRec : PRecArray = nil;
const
ARRAY_LENGTH = 500;
procedure DisposeArrayPointer;
var
i : Integer;
begin
if Assigned(parRec) then
begin
for i := Low(parRec) to High(parRec) do
if Assigned(parRec[i]) then
FreeMem(parRec[i], SizeOf(TRec));
parRec := nil; // dynamic array disposing
end;
end;
procedure FillArrayPointer;
var
i : Integer;
begin
DisposeArrayPointer();
SetLength(parRec, ARRAY_LENGTH);
for i := Low(parRec) to High(parRec) do
begin
parRec[i] := AllocMem(SizeOf(TRec));
parRec[i].Value := i;
end;
end;
procedure DisposeArray;
begin
arRec := nil; // dynamic array disposing
end;
procedure FillArray;
var
i : Integer;
begin
DisposeArray();
SetLength(arRec, ARRAY_LENGTH);
for i := Low(arRec) to High(arRec) do
arRec[i].Value := i;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FillArrayPointer;
FillArray;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DisposeArray;
DisposeArrayPointer();
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
begin
ListBox1.Clear;
for i := Low(parRec) to High(parRec) do
ListBox1.Items.Add(IntToStr(parRec[i].Value));
ListBox1.Items.Add('----------');
for i := Low(arRec) to High(arRec) do
ListBox1.Items.Add(IntToStr(arRec[i].Value));
end;
--
Regards
Theo
------------------------
Theo Bebekis
Thessaloniki, Greece
------------------------
Greek_Delphi_Prog : a Delphi Programming mailing list in Greek at
http://groups.yahoo.com/group/Greek_Delphi_Prog
CSharpDotNetGreek : A C# and .Net mailing list in Greek language at
http://groups.yahoo.com/group/CSharpDotNetGreek
atla_custom : a Unisoft Atlantis Customization mailing list at
http://groups.yahoo.com/group/atla_custom
------------------------
-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/delphi-en/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/delphi-en/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/