将 const-string 分配给常量大小的 char 数组,未使用的数组索引会发生什么?
Assigning a const-string to a constant sized char array, what happens in un-used array indices?
假设我有:
char name[16] = "123456789abc";
所以 name[11] == 'c'
, name[12] == '[=12=]'
.
name[13]
会是 gibberish/compiler-dependant,还是会可靠地成为特定值(例如“\0”?)
当从字符串文字初始化字符数组时,未使用的元素被初始化为零。
第 8.5.2 节有规则:
An array of narrow character type (3.9.1), char16_t
array, char32_t
array, or wchar_t
array can be initialized by a narrow string literal, char16_t
string literal, char32_t
string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces (2.14.5). Successive characters of the value of the string literal initialize the elements of the array.
There shall not be more initializers than there are array elements.
If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized (8.5).
因此,它们将为零。保证。
并且访问它们不是未定义的行为。
如果您从字符列表而不是 char name[16] = { '1', '2', '3', '4', '5', 0 };
进行初始化,您将处于聚合初始化领域,它通过不同的路径给出相同的结果。
当使用聚合初始化并且初始化器比聚合的元素少时,其余部分是值初始化的(除非有 brace-or-equal-initializer聚合类型的定义)。
规则见第 8.5.1 节
An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize.
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list (8.5.4).
并给出了一个例子:
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{}
(that
is, 0
), and ss.d
with the value of ss.b[ss.a]
(that is, 's'
)
C++03 没有明确声明额外的元素将在字符数组规则中被零初始化。另一方面,聚合规则基本相似并且确实保证值初始化,总是(brace-or-equal-initializer 在 C++11 中引入)。
C99 第 6.7.8 节在这两种情况下都提供了零初始化,即:
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.
当然,具有静态存储持续时间的对象被预初始化为零。
假设我有:
char name[16] = "123456789abc";
所以 name[11] == 'c'
, name[12] == '[=12=]'
.
name[13]
会是 gibberish/compiler-dependant,还是会可靠地成为特定值(例如“\0”?)
当从字符串文字初始化字符数组时,未使用的元素被初始化为零。
第 8.5.2 节有规则:
An array of narrow character type (3.9.1),
char16_t
array,char32_t
array, orwchar_t
array can be initialized by a narrow string literal,char16_t
string literal,char32_t
string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces (2.14.5). Successive characters of the value of the string literal initialize the elements of the array.There shall not be more initializers than there are array elements.
If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized (8.5).
因此,它们将为零。保证。
并且访问它们不是未定义的行为。
如果您从字符列表而不是 char name[16] = { '1', '2', '3', '4', '5', 0 };
进行初始化,您将处于聚合初始化领域,它通过不同的路径给出相同的结果。
当使用聚合初始化并且初始化器比聚合的元素少时,其余部分是值初始化的(除非有 brace-or-equal-initializer聚合类型的定义)。
规则见第 8.5.1 节
An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize.
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list (8.5.4).
并给出了一个例子:
struct S { int a; const char* b; int c; int d = b[a]; }; S ss = { 1, "asdf" };
initializes
ss.a
with1
,ss.b
with"asdf"
,ss.c
with the value of an expression of the formint{}
(that is,0
), andss.d
with the value ofss.b[ss.a]
(that is,'s'
)
C++03 没有明确声明额外的元素将在字符数组规则中被零初始化。另一方面,聚合规则基本相似并且确实保证值初始化,总是(brace-or-equal-initializer 在 C++11 中引入)。
C99 第 6.7.8 节在这两种情况下都提供了零初始化,即:
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.
当然,具有静态存储持续时间的对象被预初始化为零。