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,238 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,238 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 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools