Re: [Flashcoders] pointers, references and just straight up values
Hi Anthony, I wouldn't say AS 3 is partially managed (memory-wise). You don't have to release / free memory yourself. The garbage collector takes care of that, when an object no longer has valid references pointing to it. That doesn't mean that as soon as an object is unreachable, it' s disposed; it means it's eligible for disposal; when that actually happens depends on the GC. Some memory-greedy objects like BitmapData have a dispose method, which works like a hint to the GC, I think -- but am not sure 100% -- but in general, as long as there're no references to an object, or it becomes unreachable from your code, it'll be freed, eventually. Having said that, it's very easy to accidentally "pin down" an object, thus keeping one or more undesired references to it and preventing it from being released. You may want to check out Grant Skinner's great articles on memory and GC in AS 3.0 for more detailed info on this. Back to your example, if you pass a bitmap object to a function, you are passing a reference to that object. So, if inside that function you de-reference that reference (i.e., you use the dot operator to access the object), you're affecting the same object you can "see" outside tha function. If you create a new bitmap, then you have a new object, whether you store its reference in the same variable you used to pass the original object or not. If you stored it in the same var and you access the 2nd object through that var, yes, you're modifying the 2nd object (even though at first sight it might look like you're making changes to the first one). Cheers Juan Pablo Califano 2008/11/29, Anthony Pace <[EMAIL PROTECTED]>: > > That is absolutely the best explanation I have had so far. > > So as3 is only a partially managed language? > > For example, if I was to pass the reference to a bitmap object as an > argument to a function, it has to be dereferenced in the function by > instantiating a new variable, and that new variable could be modified > without modifying the original. > I am pretty sure I have it now, and probably sound a little retarded for > reiterating it; yet, if I am wrong please tell me. > > Thanks, > Anthony > > Juan Pablo Califano wrote: > >> Well, in AS you don't have pointers (in the sense of C pointers). Except >> for >> primitives, which are basically Number, int, uint, Boolean, and String, >> everything else is a reference to an object allocated on the heap. >> >> References in AS behave pretty much like references in Java; and probably >> you could think of them as C pointers which you just can assign / reassign >> and dereference (you can't do pointer arithmetic on them, you don't have >> something like the address-of operator and you just have one level of >> indirection). >> >> Even though you'd find the docs say objects are passed by reference, I >> think >> it'd more accurate to say that object references are passed by value (it >> may >> sound a bit pedantic perhaps, but it does make a difference as you can see >> in the following examples). >> >> Suppose you have this code: >> >> (list is an Array created outside the function and contains these values >> [0,1,2,3] ) >> >> function test(list:Array):void { >>list.push(4); >> } >> >> After the function returns, the contents of list will be [0,1,2,3,4]. >> >> You have de-referenced the parameter to access the original Array object >> and >> changed its contents using the push method. >> >> However, since objects are not passed by reference but are rather >> references >> passed by value, if you have this code: >> >> function test(list:Array):void { >>list = [0,1,2,3]; >>list.push(4); >> } >> >> After the function returns, the contents of list will be [0,1,2,3] and not >> [0,1,2,3,4]. >> >> Why? Because the list parameter is just a local variable that holds a >> reference to an object. If you de-reference it, you access to the original >> object. But if you re-assign it, you have created a new object and stored >> a >> reference to it in that variable. You've lost your "link" to the original >> object. When you access it in the next line, you are now modifiing the >> second object, not the object referenced originally in the list parameter. >> >> So, primitives have value semantics; everything else have reference >> semantics. >> >> >> Cheers >> Juan Pablo Califano >> >> >> 2008/11/29 Anthony Pace <[EMAIL PROTECTED]> >> >> >> >>> Is there a list of what is referred to as a pointer for the AS3 spec, and >>> what when a value is just duplicated? >>> >>> This would be useful to have. Took me a few moments to realize that >>> most(if not all) objects on the display list are actually pointed to when >>> passing a reference to them as a argument. >>> >>> If you know what I am looking for your would be appreciated. >>> ___ >>> Flashcoders mailing list >>> Flashcoders@chattyfig.figleaf.com >>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >>> >>> >>> >> _
Re: [Flashcoders] pointers, references and just straight up values
That is absolutely the best explanation I have had so far. So as3 is only a partially managed language? For example, if I was to pass the reference to a bitmap object as an argument to a function, it has to be dereferenced in the function by instantiating a new variable, and that new variable could be modified without modifying the original. I am pretty sure I have it now, and probably sound a little retarded for reiterating it; yet, if I am wrong please tell me. Thanks, Anthony Juan Pablo Califano wrote: Well, in AS you don't have pointers (in the sense of C pointers). Except for primitives, which are basically Number, int, uint, Boolean, and String, everything else is a reference to an object allocated on the heap. References in AS behave pretty much like references in Java; and probably you could think of them as C pointers which you just can assign / reassign and dereference (you can't do pointer arithmetic on them, you don't have something like the address-of operator and you just have one level of indirection). Even though you'd find the docs say objects are passed by reference, I think it'd more accurate to say that object references are passed by value (it may sound a bit pedantic perhaps, but it does make a difference as you can see in the following examples). Suppose you have this code: (list is an Array created outside the function and contains these values [0,1,2,3] ) function test(list:Array):void { list.push(4); } After the function returns, the contents of list will be [0,1,2,3,4]. You have de-referenced the parameter to access the original Array object and changed its contents using the push method. However, since objects are not passed by reference but are rather references passed by value, if you have this code: function test(list:Array):void { list = [0,1,2,3]; list.push(4); } After the function returns, the contents of list will be [0,1,2,3] and not [0,1,2,3,4]. Why? Because the list parameter is just a local variable that holds a reference to an object. If you de-reference it, you access to the original object. But if you re-assign it, you have created a new object and stored a reference to it in that variable. You've lost your "link" to the original object. When you access it in the next line, you are now modifiing the second object, not the object referenced originally in the list parameter. So, primitives have value semantics; everything else have reference semantics. Cheers Juan Pablo Califano 2008/11/29 Anthony Pace <[EMAIL PROTECTED]> Is there a list of what is referred to as a pointer for the AS3 spec, and what when a value is just duplicated? This would be useful to have. Took me a few moments to realize that most(if not all) objects on the display list are actually pointed to when passing a reference to them as a argument. If you know what I am looking for your would be appreciated. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] pointers, references and just straight up values
Well, in AS you don't have pointers (in the sense of C pointers). Except for primitives, which are basically Number, int, uint, Boolean, and String, everything else is a reference to an object allocated on the heap. References in AS behave pretty much like references in Java; and probably you could think of them as C pointers which you just can assign / reassign and dereference (you can't do pointer arithmetic on them, you don't have something like the address-of operator and you just have one level of indirection). Even though you'd find the docs say objects are passed by reference, I think it'd more accurate to say that object references are passed by value (it may sound a bit pedantic perhaps, but it does make a difference as you can see in the following examples). Suppose you have this code: (list is an Array created outside the function and contains these values [0,1,2,3] ) function test(list:Array):void { list.push(4); } After the function returns, the contents of list will be [0,1,2,3,4]. You have de-referenced the parameter to access the original Array object and changed its contents using the push method. However, since objects are not passed by reference but are rather references passed by value, if you have this code: function test(list:Array):void { list = [0,1,2,3]; list.push(4); } After the function returns, the contents of list will be [0,1,2,3] and not [0,1,2,3,4]. Why? Because the list parameter is just a local variable that holds a reference to an object. If you de-reference it, you access to the original object. But if you re-assign it, you have created a new object and stored a reference to it in that variable. You've lost your "link" to the original object. When you access it in the next line, you are now modifiing the second object, not the object referenced originally in the list parameter. So, primitives have value semantics; everything else have reference semantics. Cheers Juan Pablo Califano 2008/11/29 Anthony Pace <[EMAIL PROTECTED]> > Is there a list of what is referred to as a pointer for the AS3 spec, and > what when a value is just duplicated? > > This would be useful to have. Took me a few moments to realize that > most(if not all) objects on the display list are actually pointed to when > passing a reference to them as a argument. > > If you know what I am looking for your would be appreciated. > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] pointers, references and just straight up values
That's correct... AFAIK only primitive types (Strings, Boolean, Numbers, uints and ints) are passed as values. All other types are passed as references. -- Eduardo Omine http://blog.omine.net/ http://www.omine.net/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] pointers, references and just straight up values
for instance... if you modify the object that was passed as an argument in the function you have modified the object in the display list; yet, I can understand this, as without this passing around and duplicating a Bitmap object as it is passed to a function would be very costly with regard to memory. Tell me if I am off base and if my experience was a one off. Anthony Pace wrote: Is there a list of what is referred to as a pointer for the AS3 spec, and what when a value is just duplicated? This would be useful to have. Took me a few moments to realize that most(if not all) objects on the display list are actually pointed to when passing a reference to them as a argument. If you know what I am looking for your would be appreciated. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] pointers, references and just straight up values
Is there a list of what is referred to as a pointer for the AS3 spec, and what when a value is just duplicated? This would be useful to have. Took me a few moments to realize that most(if not all) objects on the display list are actually pointed to when passing a reference to them as a argument. If you know what I am looking for your would be appreciated. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders