Initialization (programming)
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, initialization is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

, as well as 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
Declaration may refer to:* Declaration , specifies the identifier, type, and other aspects of language elements* Declaration , when the captain of a cricket team declares its innings closed...

, although the two can sometimes be conflated in practice.

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. Setting a memory location to hexadecimal
Hexadecimal
In mathematics and computer science, hexadecimal is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen...

 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
Machine code
Machine code or machine language is a system of impartible instructions executed directly by a computer's central processing unit. Each instruction performs a very specific task, typically either an operation on a unit of data Machine code or machine language is a system of impartible instructions...

 level, since it requires no additional memory access.

Initializer

In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression
Expression (programming)
An expression in a programming language is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces another value...

 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 includes initialization is commonly called 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, a definition is a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common.

Examples:


int i = 0;
int i2(0);
int k[4] = {0, 1};
int j[2] = {rand, k[0]};
char tx[3]="fa";
char ty[2]="fa";
MyClass* xox = new MyClass (0, "zaza");
struct point {int x; int y;} p = {rand, i};
point q = {0, i+1};

Initializer list

A constructor
Constructor
Constructor may refer to:*Constructor , object-organizing method* Constructor , a 1997 PC game by Acclaim, the prequel of Constructor: Street Wars...

 of a class/struct can have an initializer list within the definition but prior to the constructor body. It assigns initial values to the members of the class/struct object.
Example:

struct int_complex {
int re, im;
int_complex : re(0), im(0) { }
};

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.

The new C++ standard, C++11, provides for a more powerful concept of initializer lists, by means of a template, called std::initializer_list.

Default initialization

Data initialization may occur without explicit syntax in a program to do so. For example, if static variable
Static variable
In computer programming, a static variable is a variable that has been allocated statically — whose lifetime extends across the entire run of the program...

s are declared without an initializer, then those of primitive data types are initialized with the value of zero of the corresponding type, while static objects of class type are initialized with their default constructor
Default constructor
In computer programming languages the term “default constructor” refers to a constructor that is automatically generated in the absence of explicit constructors ; this automatically provided constructor is usually a nullary constructor...

s.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK