In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, initialization or initialisation is the
assignment of an initial value for a
data object
In software development, an object is an entity that has state, behavior, and identity. An object can model some part of reality or can be an invention of the design process whose collaborations with other such objects serve as the mechanisms t ...
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.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, 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, 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. 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''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, initialization code may be part of a ''
constructor'' (class method) or an ''initializer'' (instance method). Setting a memory location to
hexadecimal
Hexadecimal (also known as base-16 or simply hex) is a Numeral system#Positional systems in detail, positional numeral system that represents numbers using a radix (base) of sixteen. Unlike the decimal system representing numbers using ten symbo ...
zeroes is also sometimes known as "clearing" and is often performed by an
exclusive or
Exclusive or, exclusive disjunction, exclusive alternation, logical non-equivalence, or logical inequality is a logical operator whose negation is the logical biconditional. With two inputs, XOR is true if and only if the inputs differ (on ...
instruction (both operands specifying the same variable), at
machine code
In computer programming, machine code is computer code consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). For conventional binary computers, machine code is the binaryOn nonb ...
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. It consists of the '=' character followed by an
expression 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 standard may refer to:
* ANSI C, C99, C11, C17, or C23, specifications of the C programming language
* C standard library
The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C pr ...
, 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 a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
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 ...
s are initialized with the value of zero of the corresponding type, while static objects of class type are initialized with their
default constructor
Default may refer to:
Law
* Default (law), the failure to do something required by law
** Default (finance)
In finance, default is failure to meet the legal obligations (or conditions) of a loan, for example when a home buyer fails to make ...
s.
See also
*
Object lifetime
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ...
*
Finalizer
In computer science, a finalizer or finalize method is a special method that performs finalization, generally some form of cleanup. A finalizer is executed during object destruction, prior to the object being deallocated, and is complementary ...
Process & related Finalization Pattern
References
{{DEFAULTSORT:Initialization (Programming)
Programming constructs
Variable (computer science)