27

Are the members of a global or static structure in C guaranteed to be automatically initialized to zero, in the same way that uninitialized global or static variables are?

tkw954
  • 940
  • 2
  • 10
  • 16

5 Answers5

40

From the C99 standard 6.7.8/10 "Initialization":

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

Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).

The C++ 2003 standard has a similar requirement (3.6.2 "Initialization of non-local objects"):

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.

Sometime after that zero-initialization takes place, constructors are called (if the object has a constructor) under the somewhat more complicated rules that govern the timing and ordering of those calls.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
8

Local variables are not initialized.

struct foobar {
    int x;
};

int main(void) {
    struct foobar qux;
    /* qux is uninitialized. It is a local variable */
    return 0;
}

static local variables are initialized to 0 (zero)

struct foobar {
    int x;
};

int main(void) {
    static struct foobar qux;
    /* qux is initialized (to 0). It is a static local variable */
    return 0;
}

Global variables are initialized to 0 (zero)

struct foobar {
    int x;
};
struct foobar qux;
/* qux is initialized (to 0). It is a global variable */

int main(void) {
    return 0;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
1

A struct is no different in this manner than a normal static C variable. The memory reserved for that struct is completely initialized to 0 if it's a static.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    Is the memory initialized to zero, or are the actual fields initialized to zero? – Victor Nicollet Nov 02 '10 at 18:08
  • Memory is initialized to zero. – Klark Nov 02 '10 at 18:12
  • @Victor: I'm not sure there's a difference, from the point-of-view of the standard. I assume you're wondering what happens to padding bits/bytes; but the value of these "doesn't matter". – Oliver Charlesworth Nov 02 '10 at 18:12
  • For instance, initializing the memory of a pointer to zero does not initialize that pointer to null. So, is the initialization a simple ZeroMemory, or does it actually do the "smart" thing and initialize every field with its actual zero value? – Victor Nicollet Nov 02 '10 at 18:18
0

Yes, all global data is zeroed out, including the members of structures, classes and unions.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
-3

All data in global part of the program is set to zero.

The BSS segment also known as Uninitialized data starts at the end of the data segment and contains all uninitialized global variables and static variables that are initialized to zero by default. For instance a variable declared static int i; would be contained in the BSS segment.

Bss segment.

I don't know why is it so hard to try it yourself btw.

Klark
  • 8,162
  • 3
  • 37
  • 61
  • 9
    Because "try it yourself" does not differentiate between standard behavior and implementation-defined behavior. – Victor Nicollet Nov 02 '10 at 18:09
  • This really isn't something that any major compiler implementation would dare to change. – Klark Nov 02 '10 at 18:15
  • 2
    Even then, knowing whether it's a standard guarantee or merely an implementation status quo is a good enough justification for asking the question. – Victor Nicollet Nov 02 '10 at 18:19