x


arrays and references - a bit puzzled here

Hi to all!

A bit of commented code should be enough to explain my problem:

    var valueArray : int[];

    function Start()
    {
         valueArray = new int[100];
         for(val in valueArray)
         {
         val=1;
         }
         var refArray : int[] = valueArray;
         print(refArray[0]); //A : this prints 1

         valueArray[0] = 2;
         print(refArray[0]); //B : this prints 2

         valueArray = new int[100];
         print(refArray[0]); //C : this still prints 2
    }

Point by point, please correct me where I'm wrong!

A: valueArray allocates 100 floats. refArray is just a pointer to valueArray.

B: this is confirmed here: changes in valueArray can be accessed through refArray.

C: Here's where I don't get it. valueArray allocates at a new adress, and ref array still points to the old one. Will the memory get garbage collected if refArray is local? Or will it cause a leak?

Many thanks for your insights!

G

more ▼

asked Jul 31 '12 at 05:59 PM

gregzo gravatar image

gregzo
1.6k 31 41 51

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It will get garbage collected when the system is ready to collect again, that will happen at any time after the last reference to refArray. You will not leak memory that way.

more ▼

answered Jul 31 '12 at 06:31 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

Thanks! I'm at peace.

Jul 31 '12 at 08:19 PM gregzo

No problem :) Can you tick it for me?

Jul 31 '12 at 08:49 PM whydoidoit

Oups, yep!

Jul 31 '12 at 11:35 PM gregzo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x291
x264

asked: Jul 31 '12 at 05:59 PM

Seen: 246 times

Last Updated: Jul 31 '12 at 11:35 PM