dhein
dhein

Reputation: 6555

Is each static array member initialized?

From ISO/IEC 9899:1999 -> 6.7.8 Initialization § 10

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

— if it is a union, the first named member is initialized (recursively) according to these rules.

Did I get this right, that imagine this code:

int main()
{
    static char *szArray[4];
    return 0;
}

it is ensured that each member of szArray[] is initialized with NULL? Or how I can understand "recursively" in this context?

Upvotes: 1

Views: 88

Answers (3)

Yes, in this case you will get an array of four NULL values.

The "recursively" in the spec does not apply to pointers. It applies to structs. So for example.

#include <stdio.h>

struct Bar {
  int yada;
};

struct Foo {
  struct Bar bar;
  const char* baz;
};

static struct Foo foo;
static struct Foo* foo_ptr;

int main()
{

  printf("foo.bar.yada = %d\n", foo.bar.yada);
  printf("foo_ptr = %p\n", foo_ptr);

  return 0;
}

Running the above gives

foo.bar.yada = 0
foo_ptr = (nil)

The initialization rule has been applied recursively to Foo and then to Bar. The pointer is simply initialized to zero.

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263557

Yes.

szArray is an array of 4 elements, each of which is a char* pointer. Each of those 4 elements is in initalized to NULL.

What "recursively" means here is that, since data types can be arbitrarily complex (arrays within structures within unions within arrays, etc.), each member of an aggregate (array or structure) is initialized following the same rules.

  • szArray is an aggregate, so "every member is initialized (recursively) according to these rules".
    • szArray[0] through szArray[3] all have pointer type, so each of them "is initialized to a null pointer".

This (probably) does not involve any run-time recursion. On most systems, integer 0, floating-point 0.0, and null pointers are all represented as all-bits-zero, so a static aggregate object can probably be correctly initialized just by setting it to all-bits-zero. It's the definition that's recursive; the initialization of an aggregate object is defined in terms of the initializations of its elements/members, and so on recursively until you get down to individual scalars.

Upvotes: 4

hyde
hyde

Reputation: 62888

Each member of szArray is indeed initialized to NULL, once, before it is used. In main this "once" does not make a difference, but in other functions which may get called many times it is important. It is especially important for thread safe code and re-entrant code, because there is just one value accessed by all calls.

Upvotes: 1

Related Questions