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 »
10,640 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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »