Content deleted Content added
No edit summary |
|||
(75 intermediate revisions by 50 users not shown) | |||
Line 1:
{{Short description|Assignment of an initial value for variable}}
In [[computer programming]], '''initialization''' or '''initialisation''' is the [[Assignment (computer science)|assignment]] of an initial value for a [[data object]] or variable. The
Initialization is done either by statically embedding the value at compile time, or else by assignment at [[Run time (program lifecycle phase)|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]], initialization code may be part of a ''[[Constructor (object-oriented programming)|constructor]]'' (class method) or an ''initializer'' (instance method). Setting a memory ___location to [[hexadecimal]] zeroes is also sometimes known as "clearing" and is often performed by an [[exclusive or]] instruction (both operands specifying the same variable), at [[machine code]] 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 (computing)|declarator]]. It consists of the '=' character followed by an [[expression (programming)|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"
A declaration which
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]], a definition ''is'' a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common.<ref>''C++ FAQs'', by Cline, Lomow, and Girou, Addison-Wesley, 1999, {{ISBN
▲A declaration which includes initialization is commonly called '''definition'''.
C examples:
▲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]], a definition ''is'' a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common.<ref>''C++ FAQs'', by Cline, Lomow, and Girou, Addison-Wesley, 1999, ISBN 0-201-30983-1.</ref>
<!-- each example deserves a separate treatment,
but I don't know how to keep a proper balance
and not to turn this page into a C++ primer
-->
<syntaxhighlight lang="c">
:int i = 0;▼
</syntaxhighlight>
: MyClass* xox = new MyClass (0, "zaza");▼
▲:struct point {int x; int y;} p = {rand(), i};
C++ examples:
:point q = {0, i+1};▼
<syntaxhighlight lang="cpp">
int j[2] = {rand(), k[0]};
</syntaxhighlight>
===Initializer list===
Example:
<syntaxhighlight lang="c">
struct IntComplex {
Here, the construct " : re(0), im(0)" is the initializer list.▼
IntComplex() : re(0), im(0) {}
int re;
int im;
};
</syntaxhighlight>
Sometimes the term "initializer list" is also used to refer to the list of expressions in the array or struct initializer.
===Default initialization===
Data initialization may occur without explicit syntax in a program to do so. For example, if [[static variable]]s are declared without an initializer, then those of [[primitive data type]]s are initialized with the value of zero of the corresponding type, while static objects of class type are initialized with their [[default constructor]]s.
== See also ==
* [[Object lifetime]]
* [[Finalizer]] Process & related Finalization Pattern
==References==
Line 44 ⟶ 60:
{{DEFAULTSORT:Initialization (Programming)}}
[[Category:Programming constructs]]
[[Category:Variable (computer science)]]
|