Object copy

In computing, a deep copy is copy that contains the complete encapsulated data of the original object, allowing it to be used independently of the original object. In contrast, a shallow copy is a copy that may be associated to data shared by the original and the copy.

For example, if a C++ class contains a pointer to a null-terminated string, the deep copy would also copy the string, while the shallow copy would create an object where the pointer points to same string, and changes to it affect both objects.

In computing, the result of shallow copying one object to another variable is two variables pointing to the same physical object in memory.

Thus changing the object pointed to by one of the variables will also cause the contents of the other variable to change (since the same object in memory is being altered).

Shallow copies are common when reference counting objects. The technique is by default when copying objects in Java.


In computer science, duplication of data or objects involves copying data byte by byte and, if needed, changing metadata about objects.

Duplication of data

In C, one can use memmove or memcopy to copy data on the memory.

Duplication of objects

A built-in method called a copy constructor can be used to easily duplicate most objects. Programmers typically implement the copy constructor as a recursive copy of all attributes and of all objects belonging to the object, one-by-one. This can take a lot of time. The duplication of lists, for instance, involves copying each element.

Implementors may set up some copy constructors with reference counting so that identical copies of an object do not take up additional memory. This technique interacts badly with low-level access to internal object data. Defensive copy is an act to ensure that the object retains a state that exists at the moment of duplication. Code here (http://www.sgi.com/tech/stl/string_discussion.html) addresses an interesting problem which relates to the implementation of string class in the STL:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   string s("abc");
   string t;
   char & c(s[1]);

   t = s;       // Data typically shared between s and t.
   c = 'z';     // How many strings does this modify?
   if (t[1] == 'z')
        cout << "wrong" << endl;
   else
        cout << "right" << endl;
}

The specification of C++ understandably frowns on changing t. One can only mitigate this problem by the use of defensive copy, but this in turn wipes out any benefit gained by the use of reference counting.

Template:Comp-sci-stub

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools