In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, initialization (or initialisation) is the
assignment
Assignment, assign or The Assignment may refer to:
* Homework
* Sex assignment
* The process of sending National Basketball Association players to its development league; see
Computing
* Assignment (computer science), a type of modification to ...
of an initial value for a
data object
In computer science, an object can be a variable, a data structure, a function, or a method. As regions of memory, they contain value and are referenced by identifiers.
In the object-oriented programming paradigm, ''object'' can be a combinatio ...
or variable. The manner in which initialization is performed depends on the
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
, as well as the type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically called initializers and initializer lists. Initialization is distinct from (and preceded by)
declaration
Declaration may refer to:
Arts, entertainment, and media Literature
* ''Declaration'' (book), a self-published electronic pamphlet by Michael Hardt and Antonio Negri
* ''The Declaration'' (novel), a 2008 children's novel by Gemma Malley
Music ...
, although the two can sometimes be conflated in practice. The complement of initialization is
finalization, which is primarily used for objects, but not variables.
Initialization is done either by statically embedding the value at compile time, or else by assignment at
run time
Run(s) or RUN may refer to:
Places
* Run (island), one of the Banda Islands in Indonesia
* Run (stream), a stream in the Dutch province of North Brabant
People
* Run (rapper), Joseph Simmons, now known as "Reverend Run", from the hip-hop group ...
. A section of code that performs such initialization is generally known as "initialization code" and may include other, one-time-only, functions such as opening files; in
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, initialization code may be part of a ''
constructor'' (class method) or an ''initializer'' (instance method). Setting a memory location to
hexadecimal
In mathematics and computing, the hexadecimal (also base-16 or simply hex) numeral system is a positional numeral system that represents numbers using a radix (base) of 16. Unlike the decimal system representing numbers using 10 symbols, hexa ...
zeroes is also sometimes known as "clearing" and is often performed by an
exclusive or
Exclusive or or exclusive disjunction is a logical operation that is true if and only if its arguments differ (one is true, the other is false).
It is symbolized by the prefix operator J and by the infix operators XOR ( or ), EOR, EXOR, , ...
instruction (both operands specifying the same variable), at
machine code
In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a very ...
level, since it requires no additional memory access.
C family of languages
Initializer
In C/C99/C++, an initializer is an optional part of a
declarator
A declarator in Scottish law is a form of legal action by which some right of property, servitude, or status
Status (Latin plural: ''statūs''), is a state, condition, or situation, and may refer to:
* Status (law)
** City status
** Legal s ...
. It consists of the '=' character followed by an
expression
Expression may refer to:
Linguistics
* Expression (linguistics), a word, phrase, or sentence
* Fixed expression, a form of words with a specific meaning
* Idiom, a type of fixed expression
* Metaphorical expression, a particular word, phrase, o ...
or a comma-separated list of expressions placed in curly brackets (braces). The latter list is sometimes called the "initializer list" or "initialization list" (although the term "initializer list" is formally reserved for initialization of class/struct members in C++;
see below).
A declaration which creates a data object, instead of merely describing its existence, is commonly called a definition.
Many find it convenient to draw a distinction between the terms "declaration" and "definition", as in the commonly seen phrase "the distinction between a ''declaration'' and ''definition''...", implying that a declaration merely designates a data object (or function). In fact, according to the
C++ standard
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, a definition ''is'' a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common.
[''C++ FAQs'', by Cline, Lomow, and Girou, Addison-Wesley, 1999, .] Although all definitions are declarations, not all declarations are definitions.
C examples:
int i = 0;
int k = ;
char tx = 'a';
char ty = 'f';
struct Point p = ;
C++ examples:
int i2(0);
int j = ;
MyClass* xox = new MyClass(0, "zaza");
point q = ;
Initializer list
In C++, a
constructor of a class/struct can have an initializer list within the definition but prior to the constructor body. It is important to note that when you use an initialization list, the values are not assigned to the variable. They are initialized. In the below example, 0 is initialized into re and im.
Example:
struct IntComplex ;
Here, the construct
: re(0), im(0)
is the initializer list.
Sometimes the term "initializer list" is also used to refer to the list of expressions in the array or struct initializer.
C++11
C++11 is a version of the ISO/ IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions b ...
provides for a
more powerful concept of initializer lists, by means of a template, called .
Default initialization
Data initialization may occur without explicit syntax in a program to do so. For example, if
static variable
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is ...
s are declared without an initializer, then those of
primitive data type
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled pr ...
s are initialized with the value of zero of the corresponding type, while static objects of class type are initialized with their
default constructor In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. I ...
s.
See also
*
Object lifetime
*
Finalizer Process & related Finalization Pattern
References
{{DEFAULTSORT:Initialization (Programming)
Programming constructs
Variable (computer science)