ColdFusion Arrays Passed By Value

Posted July 16, 2006 at 10:22 AM

Tags: ColdFusion

Dave over at edit.com came to me with a problem he was having referencing an array. He had an array stored in an instance variable of a ColdFusion component:

 Launch code in new window » Download code as text file »

  • <cfset VARIABLES.Instance.Properties = ArrayNew( 1 ) />

In one of the components functions, he was attempting to create a short-hand notation for this array so that his code would be more readable:

 Launch code in new window » Download code as text file »

  • <cfset var arrProperties = VARIABLES.Instance.Properties />

He then made changes to this arrProperties throughout the function. Later, when he dumped out the Instance variables, he saw that none of the changes he had made had held. The problem he was having here was due to the fact that in ColdFusion, arrays are passed by value, not by reference. That means that the line of code above actually duplicated the array and stored the duplicate in the variable arrProperties. Then, all changes in the function were made to the duplicate array and NOT to the original array of properties as he had intended.

This can be a bit confusing, especially if you look at the array append method:

 Launch code in new window » Download code as text file »

  • <cfset ArrayAppend( arrProperties, "test value" ) />

The ColdFusion ArrayAppend() method does NOT return a value. That means that it directly updates the array that was passed to it. The only way that this could happen would be if the array passed to ArrayAppend() was passed by reference. So, who knows what is going on underneath the hood of some of these built in ColdFusion functions. But be aware that when you pass arrays around to custom UDFs, or even set them to new variables, they are passed by value.

And a final caveat, just because an array can be duplicated does not mean that all of its values can. If you have an array that has objects that are passed by reference such as Structures or Components, the duplicate array will have pointers to the SAME objects, NOT duplicates of them. So, be aware that you might be updating the same value from two different places.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »