ColdFusion Arrays Passed By Value

Posted July 16, 2006 at 10:22 AM by Ben Nadel

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:

  • <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:

  • <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:

  • <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.



Reader Comments

Oct 1, 2010 at 11:52 AM // reply »
1 Comments

I learned this the hard way as well a few weeks ago.

I am still surprised that coldfusion does this. That is very inefficient to be constantly churning and burning arrays every time it is referenced. This might explain why the jrun.exe process ends up sucking up a lot of ram over time (well, working set??):)


Oct 3, 2010 at 9:26 PM // reply »
11,247 Comments

@Andrew,

Yeah, I also have mixed feelings about the pass-by-value approach to arrays. I think other languages do this as well; but, I am not sure what benefit of this approach is. I'd love to read up on some arguments as to by arrays should be passed by value.


Oct 10, 2010 at 11:51 AM // reply »
1 Comments

I just lost an hour on this while attempting to do an ArrayTruncate( a, len ) function. I had to return the modified array.

Passing Arrays by value is really inefficient. I bet CF is the only language doing so. I have programmed in a dozen languages and have never seen this.

The only advantage would be to prevent side effects, but this is not consistent with all ArrayXXX functions and there is therefore no way to augment consistently CF functions.


Oct 10, 2010 at 3:04 PM // reply »
11,247 Comments

@Jean,

I haven't programmed in too many languages, but I get the sense that not many others do this. I would like to see the reasoning behind this. After all, people pass structs and other "complex" objects by reference - so, it is a concept the language supports. I don't think it would be bad for the language to also support by-reference arrays.

Of course, it would be a backwards-compatibility nightmare at this point :)


Kip
Jul 8, 2011 at 12:57 PM // reply »
2 Comments

It looks like all array assignment in ColdFusion is by value... which seems really bad. Compare this array code:

  • arr = ['beginning'];
  • arr2 = arr;
  • ArrayAppend(arr2, 'end');

With similar structure code:

  • stc = {beginning='beginning'};
  • stc2 = stc;
  • stc2.middle = 'end';

Somehow ColdFusion is cheating with the built-in functions; I'd love to know how they're doing it and if there's any way to do it in my own code.


Aug 10, 2011 at 6:30 PM // reply »
7 Comments

Hi,
I have a question:
I am interacting with a Webservice which has parameter wich accepts stingArrys only
When I try to pass a string, it gives "ServiceMethodNotFoundException:" and when I pass an array its giving arugment mismatch error.
Can you let me know what should I do


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools