[Gambas-user] Question on string[]

Benoit Minisini gambas at ...1...
Wed Dec 15 11:01:25 CET 2004


On Wednesday 15 December 2004 08:53, Eilert wrote:
> Benoit Minisini schrieb:
> > On Tuesday 14 December 2004 17:55, Eilert wrote:
> >>Well, now, is it allowed or depreciated? Will I run into problems if I
> >>create a new String[] object over and over again (maybe because this sub
> >>is called again and again?).
> >>
> >>In my concrete project this is not the case, but I know how easily you
> >>can run into such problems... :-)
> >>
> >>And what do you mean "another variable has a reference on it"?
> >>
> >>Rolf
> >
> > Do you know object programming ?
>
> Well, you got me on that one :-) Actually, I know objects by theory but
> I never really got in touch with them before.
>
> > Objects are created (with NEW) and then they are always manipulated by
> > reference. Each object has a reference counter that is increased each
> > time it is affected to any variable, and decremented each time the
> > affected variable is replaced by something else.
>
> So this would increase the counter further and further and create ever
> more object references:
>
> STATIC myArray AS String[]
>
> SUB SomeButton_Click()
>
> 	myProcedure
>
> END SUB
>
> SUB myProcedure()
>
> 	myArray = NEW String[]
>
> 	'do something with it
>
> END SUB
>
>
> As myArray is static here, it will be there all the time. Each time I
> click SomeButton, there will be a new reference to it without killing
> the old reference, but the array object still is the same, or am I
> wrong? So it would be better to provide some flag to be set to avoid this:

You are wrong. You are mixing up the object itself and the reference.

NEW creates the object, and returns a reference to it (a reference is a 
pointer, i.e. its address in memory). myArray is just a variable that stored 
the pointer.

So, each time you execute myArray = NEW String[], here is what happens:

(1) A new object of class "String[]" is created.

(2) The reference counter of the object that myArray points at is decremented. 
If it is zero, then the object is freed.

(3) The contents of myArray is replaced by a pointer to the object created in 
(1).

(4) The reference counter of the object created in (1) is incremented (it was 
zero, it is now one).

I suggest you search on the internet for tutorials about OO programming, Java, 
and C (for the pointer concept).

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list