Object copy
Encyclopedia
An object copy is an action in computing where a data object has its attributes
Attribute (computing)
In computing, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such....

 copied to another object of the same data type. An object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 is a composite data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

 in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 languages. The copying of data is one of the most common procedures that occurs in computer programs. An object may be copied to reuse all or part of its data in a new context.

Methods of copying

The design goal of most objects is to give the semblance of being made out of one monolithic block even though most are not. As objects are made up of several different parts, copying becomes non trivial. Several strategies exist to attack this problem.

Consider two objects, A and B, which each refer
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

 to two memory blocks xi and yi (i = 1, 2,...). Think of A and B as strings and of xi and yi (i = 1, 2,...) as the characters they contain. There are different strategies for copying A into B.

Shallow copy

One method of copying an object is the shallow copy. In this process, B is attached to the same memory block as A. This is otherwise known as address copy.

This results in a situation in which same data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere. If the language does not have automatic garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

 the original memory block of B has probably been leaked
Memory leak
A memory leak, in computer science , occurs when a computer program consumes memory but is unable to release it back to the operating system. In object-oriented programming, a memory leak happens when an object is stored in memory but cannot be accessed by the running code...

.

The advantage of shallow copies is that their execution speed is fast and does not depend on the size of the data.

Bitwise copies of objects which are not made up of a monolithic block are shallow copies.

Deep copy

An alternative is a deep copy. Here the data are actually copied over. The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other but at the cost of a slower more expensive copy.

Lazy copy

A lazy copy is a combination of both strategies above. When initially copying an object, a (fast) shallow copy is used. A counter is also used to track how many objects share the data. When the program wants to modify an object, it can determine if the data is shared (by examining the counter) and can do a deep copy if necessary.

Lazy copy looks to the outside just as a deep copy but takes advantage of the speed of a shallow copy whenever possible. The downside are rather high but constant base costs because of the counter. Also, in certain situations, circular reference
Circular reference
A circular reference is a series of references where the last object references the first, resulting in a closed loop.-In language:A circular reference is not to be confused with the logical fallacy of a circular argument...

s can also cause problems.

Lazy copy is related to copy-on-write
Copy-on-write
Copy-on-write is an optimization strategy used in computer programming. The fundamental idea is that if multiple callers ask for resources which are initially indistinguishable, they can all be given pointers to the same resource...

.

Implementation

Nearly all object-oriented programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s provide some way to copy objects. As the majority of languages do not provide most objects themselves, the programmer has to define how an object should be copied, just as he or she has to define if two objects are identical or even comparable in the first place. Many languages provide some default behavior.

How copying is solved varies from language to language and what concept of an object it has. The following presents examples for two of the most widely used object-oriented languages, C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 and Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

, which should cover nearly every way that an object-oriented language can attack this problem.

In C++

C++ is designed so that user-defined types should be able to function exactly like native types such as char and int. One way in which C++ achieves this is by allowing any class to define a copy constructor
Copy constructor
A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed , which might be followed by parameters of any type...

 and an assignment operator. C++ provides default versions of these which perform a memberwise copy, but any class is free to disable copying or define its own copy behaviour.

In Java

Unlike in C++, objects in Java are always accessed indirectly through references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

. Objects are never created implicitly but instead are always passed or assigned by a reference variable. (Methods in Java are always pass by value, however, it is the value of the reference variable that is being passed.) The Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 manages garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

 so that objects are cleaned up after they are no longer reachable. There is no automatic way to copy any given object in Java.

Copying is usually performed by a clone method
Clone (Java method)
clone is a method in the Java programming language for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object...

 of a class. This method usually, in turn, calls the clone method of its parent class to obtain a copy, and then does any custom copying procedures. Eventually this gets to the clone method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). If this method is used, the class must implement the marker interface, or else it will throw
Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....

 a CloneNotSupportedException. After obtaining a copy from the parent class, a class's own clone method may then provide custom cloning capability, like deep copying (i.e. duplicate some of the structures referred to by the object) or giving the new instance a new unique ID.

One disadvantage is that the return type of clone is Object, and needs to be explicitly cast back
Type conversion
In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...

 into the appropriate type (technically a custom clone method could return another type of object; but that is generally inadvisable). One advantage of using clone is that since it is an overridable method
Method overriding (programming)
Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes...

, we can call clone on any object, and it will use the clone method of its actual class, without the calling code needing to know what that class is (which would be necessary with a copy constructor).

Another disadvantage is that one often cannot access the clone method on an abstract type. Most interfaces and abstract classes in Java do not specify a public clone method. As a result, often the only way to use the clone method is if the actual class of an object is known, which is contrary to the abstraction principle of using the most generic type possible. For example, if one has a List reference in Java, one cannot invoke clone on that reference because List specifies no public clone method. Actual implementations of List like ArrayList and LinkedList all generally have clone methods themselves, but it is inconvenient and bad abstraction to carry around the actual class type of an object.

Another way to copy objects in Java is to serialize
Serialization
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment...

 them through the interface. This is typically used for persistence
Persistence (computer science)
Persistence in computer science refers to the characteristic of state that outlives the process that created it. Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown....

 and wire protocol
Wire protocol
In computer networking, a wire protocol refers to a way of getting data from point to point: A wire protocol is needed if more than one application has to interoperate. In contrast to transport protocols at the transport level , the term 'wire protocol' is used to describe a common way to represent...

 purposes, but it does create copies of objects and, unlike clone, a deep copy that gracefully handles cycled graphs of objects is readily available with minimal effort from the programmer.

Both of these methods suffer from a notable problem: the constructor
Constructor (computer science)
In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...

 is not used for objects copied with clone or serialization. This can lead to bugs with improperly initialized data, prevents the use of final
Final (Java)
In the Java programming language, the final keyword is used in several different contexts to define an entity which cannot later be changed.- Final classes :...

 member fields, and makes maintenance challenging. Some utilities attempt to overcome these issues by using reflection to deep copy objects, such as generic deep copy and the deep-cloning library.

In Eiffel

Runtime objects in Eiffel
Eiffel (programming language)
Eiffel is an ISO-standardized, object-oriented programming language designed by Bertrand Meyer and Eiffel Software. The design of the language is closely connected with the Eiffel programming method...

 are accessible either indirectly through references
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

 or as expanded objects whose fields are embedded within the objects that use them. That is, fields of an object are stored either externally or internally.

The Eiffel class ANY contains features for shallow and deep copying and cloning of objects. All Eiffel classes inherit from ANY, so these features are available within all classes, and are applicable both to reference and expanded objects.

The copy feature is shown below being applied to x with an argument y.

x.copy (y)

This effects a shallow, field-by-field copy from the object associated with y to the object associated with x.

In this case no new object is created. The same objects referenced by y before the application of copy, will also be referenced by x after the copy feature completes, as in the explanation of shallow copy above.

To effect the creation of a new object which is a shallow duplicate of y, the feature twin is used. Below, a new object identical to y is created and assigned to x.

x := y.twin


The single new object is created with its fields identical to those of y.

The feature twin relies on the feature copy, which can be redefined in descendants of ANY, if necessary. The result of twin is of the anchored type like Current, as shown in the contract for twin below, excerpted from class ANY.

frozen twin: like Current
-- New object equal to `Current'
-- twin calls copy; to change copying/twining semantics, redefine copy.
ensure
twin_not_void: Result /= Void
is_equal: Result ~ Current


The anchored type declares that the type of the result of twin will be the same as the type of the target of the call ( Current ).

Deep copying and creating deep twins can be done using the features deep_copy and deep_twin, again inherited from class ANY. These features have the potential to create many new objects, because they duplicate all the objects in an entire object structure. Because new duplicate objects are created instead of simply copying references to existing objects, deep operations will become a source of performance issues more readily than shallow operations.

In other languages

In C Sharp
C Sharp
C# is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented , and component-oriented programming disciplines. It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma and ISO...

 (C#), rather than using the interface ICloneable, a generic extension method can be used to create a deep copy using reflection. This has two advantages: First, it provides the flexibility to copy every object without having to specify each property and variable to be copied manually. Second, because the type is generic, the compiler ensures that the destination object and the source object have the same type.

In Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

, the methods copy and mutableCopy are inherited by all objects and intended for performing copies; the latter is for creating a mutable type of the original object. These methods in turn call the copyWithZone and mutableCopyWithZone methods, respectively, to perform the copying. An object must implement the corresponding copyWithZone method to be copyable. To perform the copying, the copying function may call the NSCopyObject function on the object to get a byte-for-byte shallow copy.

In Objective Caml
Objective Caml
OCaml , originally known as Objective Caml, is the main implementation of the Caml programming language, created by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy and others in 1996...

, the library function Oo.copy performs shallow copying of an object.

In PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

, the clone keyword performs shallow copying of an object.http://www.php.net/manual/en/language.oop5.cloning.php Programmers may define a special method __clone in an object to provide custom copying implementation.

In Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, the library's copy module provides shallow copy and deep copy of objects through the copy and deepcopy functions, respectively. Programmers may define special methods __copy__ and __deepcopy__ in an object to provide custom copying implementation.

In Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, all objects inherit two methods for performing shallow copies, clone and dup. The two methods differ in that clone copies an object's tainted state, frozen state, and any singleton
Singleton pattern
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system...

 methods it may have, whereas dup copies only its tainted state. Deep copies may be achieved by dumping and loading an object's byte stream or YAML serialization.http://www.ruby-doc.org/docs/ProgrammingRuby/html/classes.html#S5

See also

  • Copy constructor
    Copy constructor
    A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed , which might be followed by parameters of any type...

  • Operator overloading
    Operator overloading
    In object oriented computer programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments...

  • Reference counting
    Reference counting
    In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource...

  • Copy-on-write
    Copy-on-write
    Copy-on-write is an optimization strategy used in computer programming. The fundamental idea is that if multiple callers ask for resources which are initially indistinguishable, they can all be given pointers to the same resource...

  • Clone (Java method)
    Clone (Java method)
    clone is a method in the Java programming language for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for copying an object—the assignment operator duplicates the reference, not the object...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK