36

Consider a C struct:

struct T {
    int x;
    int y;
};

When this is partially initialized as in

struct T t = {42};

is t.y guaranteed to be 0 or is this an implementation decision of the compiler?

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
VoidPointer
  • 17,651
  • 15
  • 54
  • 58

4 Answers4

48

It's guaranteed to be 0 if it's partially initialized, just like array initializers. If it's uninitialized, it'll be unknown.

struct T t; // t.x, t.y will NOT be initialized to 0 (not guaranteed to)

struct T t = {42}; // t.y will be initialized to 0.

Similarly:

int x[10]; // Won't be initialized.

int x[10] = {1}; // initialized to {1,0,0,...}

Sample:

// a.c
struct T { int x, y };
extern void f(void*);
void partialInitialization() {
  struct T t = {42};
  f(&t);
}
void noInitialization() {
  struct T t;
  f(&t);
}

// Compile with: gcc -O2 -S a.c

// a.s:

; ...
partialInitialzation:
; ...
; movl $0, -4(%ebp)     ;;;; initializes t.y to 0.
; movl $42, -8(%ebp)
; ...
noInitialization:
; ... ; Nothing related to initialization. It just allocates memory on stack.
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 1
    Great answer with the compiler example output. I it's really hard to decide whether to accept this one or bb's answer which quotes the relevant part of the spec. I wish I could accept both. – VoidPointer Apr 01 '09 at 15:51
  • 1
    Note that if the variable is global, it is guaranteed to be initialized to zero on Unix even without initializer. It is a Unix guarantee that unfortunately is not found on Windows. – Juliano Apr 01 '09 at 15:58
  • (OT) that GCC's assembly language is really ugly. – nothrow Apr 01 '09 at 18:05
  • Agreed. I hate AT&T syntax. Viva la Intel Syntax! – Mehrdad Afshari Apr 01 '09 at 18:17
  • 10
    @Juliano: That information is completely incorrect. Global variables (and all variables with static storage duration) which are not explicitly initialized are always initially zero. This is a fundamental part of the C language and has nothing to do with your OS. – R.. GitHub STOP HELPING ICE Apr 14 '11 at 22:20
  • I found a bug related to this issue. This answer has finally given me the reason to empty initialize a simple struct. I previously thought leaving it as non-initialized had the same behavior. Maybe I got confused because of the global variables as you mention. – Ruluk Feb 11 '21 at 19:52
40

item 8.5.1.7 of standard draft:

-7- If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized (dcl.init). [Example:

struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is, 0. ]

bayda
  • 13,365
  • 8
  • 39
  • 48
2

No. it is guaranteed to be 0.

nothrow
  • 15,882
  • 9
  • 57
  • 104
1

In addition to the excellent answers above, I also found out that (at least with GCC) you can "partly initialize" a struct without explicitly assigning any member by using {}:

#include <stdio.h>

struct a {
    int x;
    int y;
};

int main() {
    struct a a = {};

    printf ("{.x=%d, .y=%d}\n", a.x, a.y);
    return 0;
}

This prints out: {.x=0, .y=0}.

user3758232
  • 758
  • 5
  • 19