数组何时自动初始化为其默认数据类型?
When is an array automatically initialized to it's default datatype?
编译器何时自动将数组初始化为其默认数据类型?
我知道在这个例子中数组将被初始化,除了我期待的是 (reading here) [=13=]
char 的值而不是在我的控制台中有 spaces
(至少这是我的想法,因为什么都没有)。
#define MAX_LENGTH 25
// Array global
int vector[MAX_LENGTH];
char vector_char[MAX_LENGTH];
int main()
{
//Print int array
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << vector[i] << " "; // All outputs 0
}
cout << endl;
//Print char array and output text to see that there is
//Something printed
cout << "start";
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << vector_char[i] << " "; //All outputs...
}
cout <<"end"<< endl;
return 0;
}
我遇到的其他两种情况是数组在函数内部(其中值将是 "garbage" 或 "impredictibile" 例如:
int main()
{
int internal_vector[MAX_LENGTH];
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << internal_vector[i] << " ";
}
cout << endl;
return 0;
}
和另一个当数组是 class 的成员时(如果你尝试任何事情,你会惊讶于数组将包含垃圾值:
class MyClass
{
private:
int array[100];
...
}
那么,什么时候数组会自动初始化为它的默认数据类型?我什么时候应该创建自己的函数来初始化每个元素?
全局对象有static storage duration, they will be zero-initialized开头,
For every named variable with static or thread-local storage duration that is not subject to constant initialization (since C++14)
, before any other initialization.
If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.
If T is array type, each element is zero-initialized
也就是说vector
的所有元素都会被初始化为0
,vector_char
的所有元素都会被初始化为'[=14=]'
。
另一方面,internal_vector
中的所有元素将被初始化为default initialization中的不确定值。
if T is an array type, every element of the array is default-initialized;
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
请注意,array
有点复杂,其存储持续时间取决于实例的声明方式。
编译器何时自动将数组初始化为其默认数据类型?
我知道在这个例子中数组将被初始化,除了我期待的是 (reading here) [=13=]
char 的值而不是在我的控制台中有 spaces
(至少这是我的想法,因为什么都没有)。
#define MAX_LENGTH 25
// Array global
int vector[MAX_LENGTH];
char vector_char[MAX_LENGTH];
int main()
{
//Print int array
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << vector[i] << " "; // All outputs 0
}
cout << endl;
//Print char array and output text to see that there is
//Something printed
cout << "start";
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << vector_char[i] << " "; //All outputs...
}
cout <<"end"<< endl;
return 0;
}
我遇到的其他两种情况是数组在函数内部(其中值将是 "garbage" 或 "impredictibile" 例如:
int main()
{
int internal_vector[MAX_LENGTH];
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << internal_vector[i] << " ";
}
cout << endl;
return 0;
}
和另一个当数组是 class 的成员时(如果你尝试任何事情,你会惊讶于数组将包含垃圾值:
class MyClass
{
private:
int array[100];
...
}
那么,什么时候数组会自动初始化为它的默认数据类型?我什么时候应该创建自己的函数来初始化每个元素?
全局对象有static storage duration, they will be zero-initialized开头,
For every named variable with static or thread-local storage duration
that is not subject to constant initialization (since C++14)
, before any other initialization.If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.
If T is array type, each element is zero-initialized
也就是说vector
的所有元素都会被初始化为0
,vector_char
的所有元素都会被初始化为'[=14=]'
。
另一方面,internal_vector
中的所有元素将被初始化为default initialization中的不确定值。
if T is an array type, every element of the array is default-initialized;
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
请注意,array
有点复杂,其存储持续时间取决于实例的声明方式。