3

Consider the following code:

void func()
{
   int p;
   ...
   if (p > MAX) {
       struct my_struct s;
       ...
       /* here we access the contents 's' as '&s' */
   }
}

In this snippet s is on the stack. Is it guaranteed that the compiler initializes all structure fields to zero?

dbush
  • 205,898
  • 23
  • 218
  • 273
Mark
  • 6,052
  • 8
  • 61
  • 129

3 Answers3

4

No, it's quite the opposite.

Since s is an automatic storage local scoped (i.e., block scoped) variable, unless initialized explicitly, the contents are indeterminate.

Quoting C11, chapter §6.7.9

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [...].

However, if you want to zero-initialize the variable for an(y) aggregate type, you can simply use an initialization statement like

aggregate-type variable = {0};

which uses the following property from paragraph 21 of the same chapter, (emphasis mine)

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
4

If a variable (struct or otherwise) is declared local to a function or a containing scope (i.e. has automatic storage duration), it is not initialized in any way. You need to explicitly set the fields in the struct.

If you initialize at least one field of a struct but not all, then the remaining fields will be initialized the same as file scope variables (i.e. variables with static storage duration), which means NULL for pointer types and 0 for numeric types.

From section 6.7.9 of the C standard:

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread 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, and any padding is initialized to zero bits;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

...

21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

No, they won't be initialized at all. The structure values will end up with whatever garbage is on the stack where the structure is placed.

Sean
  • 60,939
  • 11
  • 97
  • 136