In computer programming, initialization or initialisation is the 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, 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, initialization code may be part of a 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

edit

Initializer

edit

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, a definition is a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common.[1] Although all definitions are declarations, not all declarations are definitions.

C examples:

int i = 0;
int k[4] = {0, 1};
char tx[3] = 'a';
char ty[2] = 'f';
struct Point { int x; int y; } p = { .y = 13, .x = 7 };

C++ examples:

int i2 = 0;
int i3(0);
int i4{0};
int j[2] = {rand(), k[0]};
MyClass* xox = new MyClass(0, "zaza");
Point q = {0, i + 1};

Member Initializer Lists

edit

In C++, a constructor of a class/struct can have an member initializer list within the definition but prior to the constructor body. When an initialization list is used, the values are not assigned to the variable, they are initialized. In the below example, re and im are initialized to the constructor parameters: Example:

class GaussianInteger {
private:
    int re;
    int im;
public:
    GaussianInteger(int re = 0, int im = 0): 
        re{re}, im{im} {}
};

Here, the construct re{re}, im{im} 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 more powerful concept of initializer lists, by means of a template, called std::initializer_list.

Default initialization

edit

Data initialization may occur without explicit syntax in a program to do so. For example, if static variables 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 constructors.

See also

edit

References

edit
  1. ^ C++ FAQs, by Cline, Lomow, and Girou, Addison-Wesley, 1999, ISBN 0-201-30983-1.

📚 Artikel Terkait di Wikipedia

Resource acquisition is initialization

Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented, statically typed programming languages to describe

Declaration (computer programming)

In computer programming, a declaration in a syntactic language construct is the process of specifying identifier properties for its initialization: it

Initialization

initialization in Wiktionary, the free dictionary. Initialization may refer to: Booting, a process that starts computer operating systems Initialism,

Computer program

A computer program is a sequence or set of instructions in a programming language for a computer to execute. It is one component of software, which also

Scope (computer programming)

In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program in which the

Lazy initialization

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive

Destructor (computer programming)

Class Computer programming portal new and delete (C++) Finalizer Constructor (computer science) Object lifetime Resource Acquisition Is Initialization Rule

Literate programming

Literate programming (LP) is a programming paradigm introduced in 1984 by Donald Knuth in which a computer program is given as an explanation of how it