Whatterz


The Inner Workings of a ColdFusion Array and Structure

by Simon. Average Reading Time: about 3 minutes.

Arrays and Structures are considered to be complex data types in ColdFusion. In contrast, simple data types are ones that contain a single piece of data, such as an Integer, String, or Boolean value. A complex data type can contain multiple pieces of data, which, in the case of arrays, are usually related. All the data are referenced under a single variable name. You can think of a complex variable as a variable that contains a collection of other variables inside it. An array maps Integers to arbitrarily typed objects (Integers, Strings, Booleans and Objects) while a structure, or associative array, maps arbitrarily typed objects to arbitrarily typed objects.

Arrays and structures are also known as reference types. A reference is an object containing information which refers to data stored elsewhere, as opposed to containing the data itself. References are fundamental to constructing many data structures and in exchanging information between different parts of a program. References increase flexibility in where objects can be stored, how they are allocated, and how they are passed between areas of code. As long as we can access a reference to the data, we can access the data through it, and the data itself need not be moved. They also make sharing of data between different code areas easier; each keeps a reference to it.

Array and structure reference types are pointers to a memory space. Pointers are the most primitive and error-prone but also one of the most powerful and efficient types of references, storing only the address of an object in memory. Because arrays and structures are pointers to a space in memory, it allows for easy copying of that data. However, when copying an array or structure, there is a distinct difference between how the copy is performed. This difference relates to the concepts of shallow and deep copying.

When copying an array, a deep copy is performed by ColdFusion. This means that the data is copied and a reference to the new copy is created in memory. Therefore, following the example below, a change in the value held at position 1 of array b will not over-write the value held in position 1 of array a as they have different memory references:

<cfscript>
	a = arrayNew(1);
	a[1] = 100;
	b = a; //deep copy for Array
	b[1] = 500;
	writeOutput(a[1]); // the returned result is 100
</cfscript>

We can also use the little-known Java function equals() to demonstrate that the two arrays are not equal (the references are different).

<!---
the following returns false
a and b do not refer to the same object
 --->
<cfdump var="#a.equals(b)#">

This is not the case for a structure. When copying a structure, as in the example below, a shallow copy is performed by ColdFusion. This means that a change in b.name will also result in a change of a.name:

<cfscript>
	a = structNew();
	a.name = "Simon Whatley";
	b = a; //shallow copy for Struct
	b.name = "John Doe";
	writeOutput(a.name); // the returned result is John Doe
</cfscript>

Using the equals() method, we can show that the two structures refer to the same object:

<!---
the following returns true
a and b refer to the same object
 --->
<cfdump var="#a.equals(b)#">

However, by using the duplicate() function, a deep copy of a structure can be performed:

<cfscript>
	a = structNew();
	a.name = "Wu Mingshi";
	b = duplicate(a); //deep copy for Struct
	b.name = "Ivan Ivanovic";
	writeOutput(a.name); // the returned result is Wu Mingshi
</cfscript>

The above code results in two structures being created.

Arrays and structures although similar, behave very differently. Both are known as reference types which refer to a pointer in memory. Both have the ability to contain multiple values underneath a single variable name. Both use an index to access an individual value, but the index is numeric for arrays and a arbitrary, text value for structures. Arrays are extremely useful for numeric calculations, tabular data, and data sorting. Structures, by their nature, cannot be sorted by value, only by key name. They are commonly for related data, where order is not important and direct access to an individual element is important. Many of ColdFusion’s variable scopes (such as server, application, variables, session, form etc) can be accessed as structures.

This article has been tagged

, , , , , , ,

Other articles I recommend

The Java Behind a ColdFusion Array and Structure

Following a conversation with a friend regarding how ColdFusion handles arrays and structures in ‘the background’, I was interested to find out what Java classes each were mapped to. This was a relatively simple case of using the functions getClass(), getSuperClass() and getName() to parse out the name of the Java classes.

Implicit Arrays in ColdFusion 8

A great new feature of ColdFusion 8 is its new implicit creation of Arrays and Structures. In addition to the updates to operators in ColdFusion, those of you familiar with JavaScript will recognise and welcome these changes.

An array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type. However, ColdFusion, as we will see, is not strictly typed and therefore allows any data type to be stored in combination. This allows strings, integers, booleans and other complex data types all to be stored in the same array. However, doing this certainly isn’t a good practice as it causes signification complication when accessing the stored data.

Variables of a simple data type commonly only store a single value but, in some situations, it is useful to have a variable that can store a series of related values – using an array. Arrays are described as complex data types because they can hold data in a structured, complex way.

Implicit Structures in ColdFusion 8

In an earlier post I eluded to the implicit creation of arrays in ColdFusion 8. Well, the same can be said of structures.

A structure, also known as an associative array, is a complex data type composed of a collection of keys and a collection of values, where each key is associated with one value (a key-value pair). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by a structure. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key “Age” is 29 and “City” is “London”, we say that our structure maps “Age” to 29 and “City” to “London”.

  • http://injun.ru injun #576871

    Good post!
    PS: thanks for ‘Ivan Ivanovic’… :)

  • duncan

    Either I’m being daft, or there’s a mistake in your code or comments:

    a change in b.name will also result in a change of a.name:

    a.name = "Simon Whatley";
    	b = a; //shallow copy for Struct
    	b.name = "John Doe";
    	writeOutput(a.name); // the returned result is Simon Whatley

    Should the comment say John Doe?

    
    a = structNew();
    	a.name = "Wu Mingshi";
    	b = duplicate(a); //deep copy for Struct
    	b.name = "Ivan Ivanovic";
    	writeOutput(a.name); // the returned result is Ivan Ivanovic
    

    Shouldn’t this comment say Wu Mingshi?

  • http://www.simonwhatley.co.uk Simon

    @Duncan you’re entirely correct. I’ve updated the post.

    Clearly writing this post late at night was not a very clever idea!

  • http://allremo.ru Sergey

    Very nice article, thanks

  • Pingback: Inner Workings of ColdFusion Arrays and Structures « Ecoders Resource()

  • Jimmy

    Thanks a lot for this! It just saved me a bunch of time. I wasn’t aware of CF holding a single copy of an array/struct in memory if you make a copy of it. I was ending up with 7 arrays with the same data and I couldn’t figure out why. Thanks!