C and C ++: partial initialization of the automatic structure

For example, if somestruct has three integer members, I always thought it was ok to do this in C (or C ++):

 somestruct s = {123,}; 

The first member will be initialized to 123, and the last two will be initialized to 0. I often do the same with automatic arrays, writing int arr[100] = {0,}; so that all integers in the array are initialized to zero.


I recently read in the GNU C Reference Guide :

If you do not initialize the structure variable, the effect depends on whether it has static storage (see Storage class specifications) or not. If so, members with integer types are initialized to 0 and pointer elements are initialized to NULL; otherwise, the meaning of the structure members is undefined.


Can someone please tell me what the C and C ++ standards say regarding partial automatic structure and automatic array initialization? I am doing the above code in Visual Studio without problems, but I want to be compatible with gcc / g ++ and possibly other compilers. Thanks

+57
c ++ c
May 31 '12 at 6:10
source share
5 answers

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}; //Case 1:Partial Initialization 

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

+82
May 31 '12 at 6:13
source share
β€” -

In C, objects will never be partially initialized - if any part of them is initialized, the entire object (and all sub-objects recursively are initialized). If an explicit initializer is not provided, the elements are initialized to a "null type".

The quote in your question refers to when the initializer for the whole object is completely excluded, and not when the initializer is missing in the sub-object. For example, assuming arr has an automatic storage duration, then this:

 int arr[100] = { 123 }; 

initializes arr[0] to 123 and every other element from arr to 0 . While this:

 int arr[100]; 

leaves each element arr uninitialized. It is this last case that refers to the quote.

+15
May 31 '12 at 6:31
source share

The latest versions of gcc also allow you to "partially" initialize and null at the same time:

 typedef struct{ int a,b,c; }T; T s = {0, .b=5}; 

now the members of the structure will have the following values: a=0, b=5, c=0

I have no information on whether other compilers allow this or not: p

+4
Feb 26 '13 at 19:44
source share

If the variable is global and static, it selects binary files in the global scope that are initialized to zero. If the variable is local, it is allocated on the stack, the compiler does not initialize the memory on the stack (some version of debugging can be initialized, but the release version never does this)

If a variable is allocated in Heap, the compiler does not initialize it either.

0
May 31 '12 at 6:17
source share
 // You can use something like this: typedef struct { ...; ...; } somestruct; // Declaration of struct somestruct st; // Initialising with 0. It does not depend on the size of the // structure and the number of elements in it. // memset() initialisation doesn't care if struct is static or dynamic. // but don't forget to use st instead &st to dynamic. memset(&st, 0, sizeof(somestruct)); 
0
Nov 04 '17 at 8:02
source share



All Articles