There are no Pointers in ColdFusion

We were discussing references in ColdFusion today at work and there was some confusion over what the term means. References are not pointers.

Reference != Pointer

The code below shows an example of how references work in ColdFusion/Java.

<cfset var1.data=1>

This creates a CF structure and creates a data key with a value of 1. A new object is created on the Java heap which var1 references.

<cfset var2=var1>

Creates a new variable and sets it to var1. Unlike a pointer in other languages var2 is not pointing to var1, instead var2 is a reference to the same object on the heap as var1.

<cfoutput>#var2.data#<br></cfoutput>

Since var2 references the same object as var1, you can access the structure created through var1.

<cfset var2.data=2>
<cfoutput>#var1.data#</cfoutput>

If you change the value using var2, the data is also changed for var1. They are both referencing the same object.

<cfset var1="Test">
<cfoutput>#var1#<br>#var2.data#</cfoutput>

This creates a new String Object on the Java heap and changes var1's reference to this new object, var2 is unaffected and continues to reference to the structure object.

I hope this clears up some of the confusion about ColdFusion references.

Comments
Comments are not allowed for this entry.
BlogCFC was created by Raymond Camden. This blog is running version 5.004.