Skip to main content
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Joshua Ford
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Joshua Ford

ColdFusion Arrays Passed By Value

By
Published in Comments (6)

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.

Want to use code from this post? Check out the license.

Reader Comments

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??):)

15,841 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.

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.

15,841 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 :)

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.

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

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel