andersch.dev

<2025-04-20 Sun>

Zero-Is-Initialization (ZII)

Zero-Is-Initialization (ZII) is a memory initialization strategy where having zero-ed out memory be the default initialization for all types. It can be contrasted with RAII.

With ZII, any codepath receiving zeroed out input should still behave sensibly (i.e. not error out).

Simple Example

struct string_t {
    u8 *str;
    u64 size;
};

A zero-initialized string_t will be interpreted as an empty string (since the size is 0). This way, the str nullpointer will never be dereferenced.

Processing Example

struct MD_TokenArray
{
   MD_Token *v;
   U64 count;
};
struct MD_MsgList
{
   MD_Msg *first, *last;
   U64 count;
   MD_MsgKind worst_message_kind;
};
struct MD_TokenizeResult
{
   MD_TokenArray tokens;
   MD_MsgList msgs;
};
MD_TokenizeResult MD_TokenizeFromText(Arena *arena, String8 text);


/* processing types returned from MD_TokenizeFromText */
/* Loops are delimited by a null first pointer or by a zero count */
MD_TokenizeResult tokenize = MD_TokenizeFromText(arena, text);
for(MD_Msg *msg = tokenize.msgs.first; msg != 0; msg = msg->next)
{
    // print out `msg` info
}
for(U64 token_idx = 0; token_idx < tokenize.tokens.count; token_idx += 1)
{
    // use `tokenize.tokens.v[token_idx]`
}

Resources