Clone (function)
From Free net encyclopedia
←Older revision | Newer revision→
clone()
is a function in the C programming language on Linux related to multithreading. clone()
is also a method in the Java programming language for object duplication.
Linux C clone
function
The syntax for calling clone
in a Linux C program is:
#include <sched.h> int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
clone
creates a new thread that starts with function pointed to by the fn
argument (as opposed to fork() which continues with the next command after fork().) The child_stack
argument is a pointer to a memory space to be used as the stack for the new thread (which must be malloc'ed before that; on most architectures stack grows down, so the pointer should point at the end of the space), flags
specify what gets inherited from the parent process, and arg
is the argument passed to the function. It returns the process ID of the child process or -1 on failure.
Java clone
method
The Java class Template:Javadoc:SE contains a clone()
method that creates and returns a copy of the object. By default, classes in Java do not support cloning and the default implementation of clone
throws a Template:Javadoc:SE. Classes wishing to allow cloning must implement the marker interface Template:Javadoc:SE. Since the default implementation of Object.clone
only perfoms a shallow copy, classes must also override clone to provide a custom implementation when a deep copy is desired.
The syntax for calling clone
in Java is:
Object copy = obj.clone();
or commonly
MyClass copy = (MyClass) obj.clone();
which provides the typecasting needed to assign the generic Object
reference returned from clone
to a reference to a MyClass
object.