liveBook · Manning

Initialization

As explained in Modern C

An initialization function is a function conventionally responsible for putting a supplied object into a valid initial state.

Initialize an allocated object

Consider `rat const* myRat = rat_init(malloc(sizeof(rat)), 13, 7);`. `malloc` supplies storage; `rat_init` puts the supplied object into its initial state using the two values. Allocation and initialization are separate jobs, even though this expression performs them together. section 13.4

An initialization function is the conventional place to establish a valid initial state for an object. For a type named `toto`, name it `toto_init` and pass a pointer to the object first. Further arguments may supply initial member values. Under this convention, a null object pointer makes the function do nothing; it returns the received pointer, or `0` on error. section 13.4

Why use a function rather than an initializer? A declaration such as `size_t i = 0;` gives an object its initial value as it is defined. Brace-enclosed initializers also handle arrays, and omitted elements become zero. But a variable-length array or a dynamically allocated array cannot be initialized that way. Assigning every element at each use site separates setup from use and is harder to maintain; an initialization function keeps that work together. section 2.3 section 13.4

 1   rat* rat_vinit(size_t n, rat p[n]) {
 2     if (p)
 3       for (size_t i = 0; i < n; ++i)
 4         rat_init(p+i, 0, 1);
 5     return p;
 6   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":3,\"ch\":28},{\"line\":3,\"ch\":29}],[{\"line\":0,\"ch\":20},{\"line\":0,\"ch\":26}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":9}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":2,\"ch\":9},{\"line\":2,\"ch\":12}],[{\"line\":0,\"ch\":20},{\"line\":0,\"ch\":26}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":20}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!

For an array of `rat` objects, `rat_vinit` checks the pointer, calls `rat_init` on each of its `n` elements, then returns the pointer. The loop makes explicit the work that an array initializer cannot do for dynamically allocated storage. section 13.4

Storage duration matters. Objects with static or thread storage duration receive default initialization equivalent to initializing their members by zero. Objects with automatic or allocated storage duration need explicit initialization. Initialize variables so that access finds a known value; for most non-variable-length object types, `T a = {0}` provides a simple starting point. section 13.4 section 5.5

TAKEAWAY: Provide an initialization function systematically for each data type, with a consistent name and calling convention. A `rat_new` function can then combine `malloc` with `rat_init`; a corresponding array function can combine allocation with `rat_vinit`. Do not trade a well-defined initial state for presumed performance: compilers can often remove unnecessary initialization. section 13.4 section 5.5 section 15.0

How Modern C covers it

How deep it goes
Specifies naming, arguments, null handling, return values, and an element-by-element array routine; does not detail the internals of `rat_init`.
Examples
Uses scalar and designated array initializers, then `rat_init`, `rat_new`, and an array initialization loop.
What it assumes
Introduces declaration initializers through simple examples; assumes pointers and allocation when developing initialization functions.
What you can do after
Build it from scratch: define a consistent `_init` function and use it to initialize allocated objects or arrays.
How it connects
Connects initialization functions to storage duration, declaration initializers, variable-length arrays, pointers, and dynamic allocation.

Also explained in

Compare how 5 books explain initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.