ColdFusion Structures are Passed By Reference
ColdFusion makes it quite easy to work with what is known in many other languages as associative arrays; ColdFusion calls them structs or structures. A structure is a great datatype to work with as long as you know how to use it.
One of the things that you must remember about this special datatype is something that I ran into today when building a Validation CFC for a form I am working on for a client.
First a little background…ᯀ t
ᯀ t
In this component, I call a function called getData(). It is called after the instantiation of the component when supplied with a formStruct which contains all the form elements and values that were submitted.
During the course of the Validation process for a given form, I want to store a little more information about each form element other than the name and value (which is all that is provided when a user submits a form). My getData() function extracts each form element and it’s value from the structure and creates a new structure with attributes fieldName, data, isValidEntry. The first two are self-explanatory, however, the third attribute is a flag that specifies whether the form element is valid for it’s specified data type (e.g. email, phone number, credit card etc.). This attribute is initially set to true as it has not been validated just yet.
After the function extracts each form field into a separte structure, it places them into an array that will contain all the form element structures. This makes it quite easy to loop through all the data and validate it based on the form element name.
Now that I have thoroughly introduced my situation, I will tell you about the problem I came across. The following code is what my getData() function looked like inside my Validation component:
This appears that it would be correct and wouldn’t have any issues. Indeed when I gave it a test, it did not error out, however, when I CFDUMP’ed the array that was created, every single structure contained the EXACT same information. In fact, it contained the data for my submit button. But “Why?”, I asked.
Going back and looking at my code, I saw the line that read:
It was at this point that it clicked. ColdFusion structures are passed to functions BY REFERENCE. That means regardless where that structure get’s passed, you are referring the actual structure in memory. No matter where it is changed, it changes the original data and all references to it are updated. Every time I was looping through each element in the array, I would change the data in the original structure. And because I was simply passing the name of structure in the arrayAppend function, only a reference to the structure was being passed.
Naturally there a couple of ways around this. The first solution was the one I went with, I had to rewrite my code as follows, creating a new structure on each pass of the loop:
Whilst writing this post, I realized that I probably could have just changed the arrayAppend function to read:
This would have created a shallow copy (only simple variables and arrays) of the temporary structure. I may very well do this afterall.
The lesson here is, be aware of how ColdFusion deals with various data types. Ensure that you know which data types are passed by reference and which are passed by value.







Just had the same issue. I knew about it,
but forgot. Thanks for the post.