Csc.216/s08/cultivate virtue: Difference between revisions
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== '''Deep Copies vs. Shallow Copies: Active Learning Exercise''' == | == '''Deep Copies vs. Shallow Copies: Active Learning Exercise''' == | ||
In object oriented languages, it is often necessary to copy (or clone) an entire object, including all of the object's member variables. It is necessary, therefore, to copy each of the member variables individually. But be careful! It is often not enough to simply use the assignment operator, as this will likely only reference the second object, instead of actually copying the memory. Such an occurrence is known as a shallow copy, and can be the source of much grief if not properly accounted for. The solution to this phenomenon is called a deep copy, and simply requires the allocation of new memory for each and every member variable in the class being copied. | In object oriented languages, it is often necessary to copy (or clone) an entire object, including all of the object's member variables. It is necessary, therefore, to copy each of the member variables individually. But be careful! It is often not enough to simply use the assignment operator, as this will likely only reference the second object, instead of actually copying the memory. Such an occurrence is known as a '''shallow copy''', and can be the source of much grief if not properly accounted for. The solution to this phenomenon is called a '''deep copy''', and simply requires the allocation of new memory for each and every member variable in the class being copied. | ||
In order to more completely illustrate this concept to a group of students, have them participate in the following activity. | In order to more completely illustrate this concept to a group of students, have them participate in the following activity. | ||
1. Obtain several pieces of paper (any type), a block of sticky notes, a bundle of string, a pen or pencil, and some clear tape. | 1. Obtain several pieces of '''paper''' (any type), a block of '''sticky notes''', a bundle of '''string''', a pen or '''pencil''', and some clear '''tape'''. | ||
These objects will represent classes, member variables, and references, respectively. The tape is simply to hold the yarn | These objects will represent classes, member variables, and references, respectively. The tape is simply to hold the yarn | ||
in place. | in place. | ||
Line 23: | Line 22: | ||
reference strings which point to nothing. This illustrates what happens when the original object is removed from memory. | reference strings which point to nothing. This illustrates what happens when the original object is removed from memory. | ||
6. Prompt the class for what must be done in order to alleviate this problem, and if desired, illustrate this deep copy by | 6. Prompt the class for what must be done in order to alleviate this problem, and if desired, illustrate this '''deep copy''' by | ||
bringing back the original object and creating new sticky note variables for the new object. | bringing back the original object and creating new sticky note variables for the new object. | ||
This learning exercise provides a visual example of the differences between deep and shallow copies, as well as highlighting the dangers of shallow copies. | This learning exercise provides a visual example of the differences between deep and shallow copies, as well as highlighting the dangers of shallow copies. | ||
Created by Mark Etheridge and Nick Forrer | Created by '''Mark Etheridge''' and '''Nick Forrer''' |
Latest revision as of 03:41, 16 April 2008
Deep Copies vs. Shallow Copies: Active Learning Exercise
In object oriented languages, it is often necessary to copy (or clone) an entire object, including all of the object's member variables. It is necessary, therefore, to copy each of the member variables individually. But be careful! It is often not enough to simply use the assignment operator, as this will likely only reference the second object, instead of actually copying the memory. Such an occurrence is known as a shallow copy, and can be the source of much grief if not properly accounted for. The solution to this phenomenon is called a deep copy, and simply requires the allocation of new memory for each and every member variable in the class being copied.
In order to more completely illustrate this concept to a group of students, have them participate in the following activity.
1. Obtain several pieces of paper (any type), a block of sticky notes, a bundle of string, a pen or pencil, and some clear tape. These objects will represent classes, member variables, and references, respectively. The tape is simply to hold the yarn in place.
2. Cut several pieces of yarn approximately three feet long, and set to the side.
3. Label a piece of paper with the desired class name, and create several variables for the class by labeling sticky notes with desired variable names and sticking them to the paper.
4. Have your students illustrate a shallow copy by creating another piece of paper (with the same name) and, rather than using sticky notes for the variables, simply connecting the new object to the variables of the old object using the string and tape (do not tape the ends of the string to the old object variables, however).
5. At this point, is should be clear to the entire group that the variables do not actually belong to the new object, but are simply referenced by it. To illustrate this point even more fully, remove the old object, leaving the new object with reference strings which point to nothing. This illustrates what happens when the original object is removed from memory.
6. Prompt the class for what must be done in order to alleviate this problem, and if desired, illustrate this deep copy by bringing back the original object and creating new sticky note variables for the new object.
This learning exercise provides a visual example of the differences between deep and shallow copies, as well as highlighting the dangers of shallow copies.
Created by Mark Etheridge and Nick Forrer