The gcc related documentation does not talk about Partial Initialization , it just talks about (Full) Initialization or No Initialization .
What is partial initialization?
Standards do not define partial initialization of objects, either full initialization is performed, or there is no initialization. Partial initialization is a non-standard terminology that usually refers to a situation where you provide some initializers, but not all. I: Fewer initializers than the size of the array or the number of initialized structure elements.
Example:
int array[10] = {1,2};
What is initialization (completed) or not initialization?
Initialization means providing some initial value for a variable created at the same time that it is being created. i.e.: in the same code expression.
Example:
int array[10] = {0,1,2,3,4,5,6,7,8,9}; //Case 2:Complete Initialization int array[10]; //Case 3:No Initialization
This paragraph describes the behavior for Case 3 .
Partial initialization rules ( Case 1 ) are well defined by the standard, and these rules are independent of the type of storage of the variable being initialized.
AFAIK. All major compilers follow these rules 100%.
Can someone tell me what they say about C and C ++ standards regarding partial automatic structure and automatic array initialization?
The C and C ++ standards guarantee that even if the integer array is in automatic storage, and if the list enclosed in brackets has fewer initializers, then uninitialized elements are initialized by 0 .
C99 Standard 6.7.8.21
If the list enclosed in brackets contains fewer initializers than the element or elements of the collection or fewer characters in the string literal used to initialize an array with a known size than in the array, the rest of the aggregate must be initialized implicitly in the same way as objects, having a static storage duration.
In C ++, rules are indicated with a slight difference.
C ++ 03 Standard 8.5.1 Aggregates
Paragraph 7:
If the list has fewer initializers than members in the aggregate, then each element that is not explicitly initialized should be initialized with the value (8.5). [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 . ]
While the Initialization value is being initialized,
C ++ 03 8.5 Initializers
Paragraph 5:
In value-initialize, an object of type T means:
- if T is a class type (section 9) with a constructor declared by the user (12.1), then the default constructor for T is called (and initialization is poorly formed if T does not have an accessible default constructor); - if T is the type of a non-unit class without a constructor declared by the user, then each non-static data element and components of the base class T are initialized with a value;
- if T is an array type, then each element is initialized with a value; - otherwise the object is initialized to zero