Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-03

two questions about dynamic array

13 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

Hi All,

I have two simple questions:

1) As far as I know class variables in FPC are automatic initialize to the NULL value (e.g. 0 or "" or whatever), does that include dynamic arrays? e.g.

TMyClass = class
    myarray: array[0..100] of Integer;
end;

will myarray be initialized to nil?

2) After SetLength(myarray, 0), is myarray nil or pointing to some invalid address?

Thanks

Xiangrong Fang
to Xiangrong Fang

Sorry the array should be dynamic, without [0..100].

2013/3/12 Xiangrong Fang

Show quoted text 20 lines
Hi All, I have two simple questions: 1) As far as I know class variables in FPC are automatic initialize to the NULL value (e.g. 0 or "" or whatever), does that include dynamic arrays? e.g. TMyClass = class myarray: array[0..100] of Integer; end; will myarray be initialized to nil? 2) After SetLength(myarray, 0), is myarray nil or pointing to some invalid address? Thanks
Howard Page-Clark
to Xiangrong Fang
Xiangrong Fang wrote
TMyClass = class myarray: array[0..100] of Integer; end;

You could answer such questions yourself:

program Project1;
{$mode objfpc}
type
TMyClass = class
   myarray: array of Integer;
end;
var my:TMyClass;
begin
   my:=TMyClass.Create;
   try
    writeln('After creation the value of my.myarray is
',PtrUInt(my.myarray));
    SetLength(my.myarray, 0);
    writeln('After SetLength(), value of my.myarray is
',PtrUInt(my.myarray));
   finally
     my.Free;
   end;
   ReadLn;
end.
Xiangrong Fang
to Sven Barth: invalid address? It will be Nil. An equivalent alternative would be: MyArray := Nil; Sven

2013/3/13 Sven Barth

Show quoted text 8 lines
Am 13.03.2013 10:08 schrieb "Xiangrong Fang" : > 2) After SetLength(myarray, 0), is myarray nil or pointing to some invalid address? It will be Nil. An equivalent alternative would be: MyArray := Nil;
Sorry, I didn't see this reply just now...   You mean, by MyArray := nil;
but NOT calling SetLength(MyArray, 0); there will NOT be memory leaks?
Howard Page-Clark
to Mattias Gaertner: On Wed, 13 Mar 2013 09:35:43 +0000 Not with this simple test. Mattias
Mattias Gaertner wrote 17 lines
On Wed, 13 Mar 2013 09:35:43 +0000 Howard Page-Clark wrote: > On 12/03/13 8:30, Xiangrong Fang wrote: >> TMyClass = class >> myarray: array[0..100] of Integer; >> end; > You could answer such questions yourself: Not with this simple test. > [...] > my:=TMyClass.Create; > try > writeln('After creation the value of my.myarray is > ',PtrUInt(my.myarray));

Could you show a more complex test that would give a satisfactory answer to the OP's questions?

Howard

Sven Barth
to Xiangrong Fang: 2013/3/13 Sven Barth Sorry, I didn't see this reply just now... You mean, by MyArray :=…
"Xiangrong Fang" wrote 5 lines
2013/3/13 Sven Barth > > Am 13.03.2013 10:08 schrieb "Xiangrong Fang" : > > 2) After SetLength(myarray, 0), is myarray nil or pointing to some

invalid address?

Show quoted text 7 lines
> > It will be Nil. An equivalent alternative would be: > > MyArray := Nil; Sorry, I didn't see this reply just now... You mean, by MyArray := nil;

but NOT calling SetLength(MyArray, 0); there will NOT be memory leaks?

As dynamic arrays are reference counted they are equal.

Regards,
Sven

Mattias Gaertner
to Howard Page-Clark: Could you show a more complex test that would give a satisfactory answer to the OP's…
Show quoted text 11 lines
On 13/03/13 9:50, Mattias Gaertner wrote: > On Wed, 13 Mar 2013 09:35:43 +0000 > Howard Page-Clark wrote: > >> On 12/03/13 8:30, Xiangrong Fang wrote: >>> TMyClass = class >>> myarray: array[0..100] of Integer; >>> end; [...] Could you show a more complex test that would give a satisfactory answer to the OP's questions?

Xiangrong wanted to know if the above array is initialized. If you want to test that then you have to either single step via the debugger or to compare the allocated memory before and after. The problem is that you do not know the memory position before the allocation.
You can try to create the class, change it, free it and allocate it again. If the two instances are at the same position, then you can check for 0 or nil.

Mattias

Howard Page-Clark
to Mattias Gaertner
Mattias Gaertner wrote 8 lines
Xiangrong wanted to know if the above array is initialized. If you want to test that then you have to either single step via the debugger or to compare the allocated memory before and after. The problem is that you do not know the memory position before the allocation. You can try to create the class, change it, free it and allocate it again. If the two instances are at the same position, then you can check for 0 or nil.

Thanks for the clear explanation. I now see that the question is about the data stored in memory, not about the memory address.

Howard