Content deleted Content added
ClueBot NG (talk | contribs) m Reverting possible vandalism by 82.33.220.162 to version by 2620:0:1000:2500:D10C:970E:616C:FB9A. False positive? Report it. Thanks, ClueBot NG. (2163472) (Bot) |
No edit summary |
||
(43 intermediate revisions by 29 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 manner in which initialization is performed depends on the [[programming language]], 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 (computer programming)|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 (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.
Line 5 ⟶ 6:
==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
C examples:
Line 15 ⟶ 16:
and not to turn this page into a C++ primer
-->
<
int i = 0;
int k[4] = {0, 1};
Line 21 ⟶ 22:
char ty[2] = 'f';
struct Point {int x; int y;} p = { .y = 13, .x = 7 };
</syntaxhighlight>
C++ examples:
<
int i2(0);
int j[2] = {rand(), k[0]};
MyClass* xox = new MyClass
point q = {0, i + 1};
</syntaxhighlight>
===Initializer list===
In C++, a [[constructor (object-oriented programming)|constructor]] of a class/struct can have an '''initializer list''' within the definition but prior to the constructor body. It
Example:
<
IntComplex()
int im;
};
</syntaxhighlight>
Here, the construct <code> : re(0), im(0)</code> 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]] provides for a [[C++11#Initializer_lists|more powerful concept of initializer lists]], by means of a template, called
===Default initialization===
Line 58 ⟶ 61:
{{DEFAULTSORT:Initialization (Programming)}}
[[Category:Programming constructs]]
[[Category:Variable (computer
|