Assigning a const string to a constant size char array, what happens in unused array indices?

Let's say I have:

char name[16] = "123456789abc";

      

SO name[11] == 'c'

, name[12] == '\0'

.

Will it name[13]

be gibberish / compiler dependent or will it reliably be a specific value (e.g. '\ 0'?)

+3


source to share


1 answer


When a character array is initialized from a string literal, unused elements are initialized to zero.

Section 8.5.2 states the rule:

A narrow character array array (3.9.1), an char16_t

array char32_t

or array wchar_t

can be initialized with a narrow string literal, char16_t

string literal, char32_t

string literal, or wide string literal, respectively, or an appropriately printed string literal enclosed in curly braces (2.14.5 ). Consecutive characters of the string literal value initialize the elements of the array.

There should be no more initializers than array elements here.

If the number of initializers is less than the number of elements in the array, each element not explicitly initialized must be initialized to zero (8.5).

Therefore, they will be zero. Guaranteed.

And accessing them is not undefined behavior.


If you initialized from a symbol list instead char name[16] = { '1', '2', '3', '4', '5', 0 };

, you would be in the area of ​​aggregate initialization, which gives the same result via a different route.

When aggregate initialization is used and the number of initializers is less than the number of members in the aggregate, the remainder is value-initialized (unless there is an equalized or equal member in the aggregate type definition).

Rule found in section 8.5.1



The initializer list is poorly formed when the number of arguments to the initializer exceeds the number of elements or elements to initialize.

If the list has fewer initializer clauses than the aggregate has members, then each element that is not explicitly initialized must be initialized from its own equalized or equal-initializer element, or, if there is no parenthesis-or-equal-initializer, from the empty list initializers (8.5.4).

And an example is given:

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

      

initializes ss.a

with 1

, ss.b

with "asdf"

, ss.c

with the value of an expression of the form int{}

( , what,0

) and ss.d

with the value ss.b[ss.a]

(i.e. 's'

)


It is not explicitly stated in C ++ 03 that additional elements will be zero-initialized in the character array rule. On the other hand, the aggregate rule was essentially similar and always guaranteed value initialization (in C ++ 11, (sliding or equal-initializer) was introduced.

C99 section 6.7.8 provides null initialization in both cases, for example:

If the list enclosed in curly braces has fewer initializers than the elements or elements of a collection, or fewer characters in the string literal used to initialize an array of known size than there are elements in the array, the rest of the collection must be implicitly initialized in the same way as objects having static storage duration.

Objects with static storage duration are, of course, pre-initialized to zero.

+8


source







All Articles