argument returning

J

josh

Hi, if I have a method like this:

public static int[][] pass()
{
int a[][] = {{1,2},{3,4}};

return a;
}

and than

int z[][] = pass();

when I call this method it returns the value of 'a' that is
a pointer (reference) to the matrix created. So in the caller
z has that value.
But I have a doubt, when the method return the variable 'a' is garbaged
and
thus also its value so how is possible that z has that value?
May be because the method returns to 'z' a copy of the 'a' value like
any variable value?

In the c/c++ when I return a matrix or a pointer in the function that
variable must be set
as static otherwise it loose its value...

Is that wrong?

Thanks
 
M

Manish Pandit

Hi,

What you get is a copy of a. The method has 'a' declared as a local
variable, and it is method-scoped. Variable z[][] will be {{1,2},{3.4}}
till you explicitly change it.

-cheers,
Manish
 
J

josh

Manish Pandit ha scritto:
Hi,

What you get is a copy of a.

that it is a reference that point to that matrix values ...
The method has 'a' declared as a local
variable, and it is method-scoped.

so when it returns a is garbaged ...
Variable z[][] will be {{1,2},{3.4}}
till you explicitly change it.

because it now point to the reference copy pointed by 'a' ...

Correct interpretation?
 
M

Matt Humphrey

josh said:
Manish Pandit ha scritto:


that it is a reference that point to that matrix values ...

Yes, you get a copy of the reference to object. There is still only one
object.
so when it returns a is garbaged ...

"a" is not garbage collected. It is a variable (a name) and it goes out of
scope at the end of the method. This gives rise to the possibility that
whatever it was pointing to may become inaccessible (and hence eligible for
garbage collection), but the matrix will not be eligible for collection
because there is still a live reference to it. The garbage collector will
reclaim eligible objects at its discretion later.
Variable z[][] will be {{1,2},{3.4}}
till you explicitly change it.

because it now point to the reference copy pointed by 'a' ...

z points to the same (identity) object that a pointed to.
Correct interpretation?

Better.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

josh

Matt Humphrey ha scritto:
"a" is not garbage collected. It is a variable (a name) and it goes out of
scope at the end of the method. This gives rise to the possibility that
whatever it was pointing to may become inaccessible (and hence eligible for
garbage collection), but the matrix will not be eligible for collection
because there is still a live reference to it. The garbage collector will
reclaim eligible objects at its discretion later.

so only objects when have not more reference to them will be garbaged
and not the variables in which there are their references.
 
M

Matt Humphrey

josh said:
Matt Humphrey ha scritto:


so only objects when have not more reference to them will be garbaged
and not the variables in which there are their references.

I think you've got it--it's a matter of distinguishing between the variable,
the reference and the object. Variables hold references to objects and as
long as the variable is in scope, the object can be reached. When the
variable goes out of scope the reference it was holding disappears but the
object still stays. The object will only be eligible for garbage collection
when it can no longer be reached from any point in the program.

I find pictures helpful for this-- http://www.iviz.com/memory pointer.gif
A variable going out of scope is the same as having its box (on the diagram)
disappear. It doesn't change the objects at all, but if there are no longer
any arrows to objects, that object is eligible for garbage collection.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

josh

Matt Humphrey ha scritto:
I think you've got it--it's a matter of distinguishing between the variable,
the reference and the object. Variables hold references to objects and as
long as the variable is in scope, the object can be reached. When the
variable goes out of scope the reference it was holding disappears but the
object still stays. The object will only be eligible for garbage collection
when it can no longer be reached from any point in the program.

Thanks the answer has been very clear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top