静态布尔数组未按设置初始化
Static Bool Array Not initializing as set
为什么我的静态布尔数组没有正确初始化?只有第一个被初始化 - 我怀疑这是因为数组是静态的。
下面的 MWE 是用 GCC 编译的,基于我正在编写的一个函数,我已经将它转移到一个主程序中来说明我的问题。我尝试过使用和不使用 c++11。我的理解是因为这个数组是静态的并且初始化为 true 这应该总是在我第一次进入我的函数时打印出来。所以在这个 MWE 中它应该打印一次。
#include <iostream>
using namespace std;
const int arraysize = 10;
const int myIndex = 1;
static bool firstTimeOverall = true;
int main()
{
static bool firstCloudForThisClient[arraysize] = {true};
cout.flush();
if (firstCloudForThisClient[myIndex])
{
cout << "I never get here" << endl;
firstCloudForThisClient[myIndex] = false;
if (firstTimeOverall)
{
firstTimeOverall = false;
cout << "But think I would get here if I got in above" << endl;
}
}
return 0;
}
您使用 array[size] = {true}
仅初始化数组的第一个元素,如果 arraysize 变量大于 1,则其他元素的初始值取决于平台。我认为这是一个未定义的行为。
如果您确实需要初始化数组,请改用循环:
for(int i=0; i < arraysize; ++i)
firstCloudForThisClient[i] = true;
您可能需要反转条件以利用默认初始化:
#include <iostream>
using namespace std;
const int arraysize = 10;
const int myIndex = 1; // note this index does not access the first element of arrays
static bool firstTimeOverall = true;
int main()
{
static bool firstCloudForThisClient[arraysize] = {}; // default initialise
cout.flush();
if (!firstCloudForThisClient[myIndex])
{
cout << "I never get here" << endl;
firstCloudForThisClient[myIndex] = true; // Mark used indexes with true
if (firstTimeOverall)
{
firstTimeOverall = false;
cout << "But think I would get here if I got in above" << endl;
}
}
return 0;
}
static bool firstCloudForThisClient[arraysize] = {true};
这会将第一个条目初始化为 true,将所有其他条目初始化为 false。
if (firstCloudForThisClient[myIndex])
但是,由于 myIndex
是 1 并且数组索引是 zero-based,这会访问 second 条目,这是错误的。
您应该访问数组的第一个元素,因此使用:
const int myIndex = 0;
为什么我的静态布尔数组没有正确初始化?只有第一个被初始化 - 我怀疑这是因为数组是静态的。
下面的 MWE 是用 GCC 编译的,基于我正在编写的一个函数,我已经将它转移到一个主程序中来说明我的问题。我尝试过使用和不使用 c++11。我的理解是因为这个数组是静态的并且初始化为 true 这应该总是在我第一次进入我的函数时打印出来。所以在这个 MWE 中它应该打印一次。
#include <iostream>
using namespace std;
const int arraysize = 10;
const int myIndex = 1;
static bool firstTimeOverall = true;
int main()
{
static bool firstCloudForThisClient[arraysize] = {true};
cout.flush();
if (firstCloudForThisClient[myIndex])
{
cout << "I never get here" << endl;
firstCloudForThisClient[myIndex] = false;
if (firstTimeOverall)
{
firstTimeOverall = false;
cout << "But think I would get here if I got in above" << endl;
}
}
return 0;
}
您使用 array[size] = {true}
仅初始化数组的第一个元素,如果 arraysize 变量大于 1,则其他元素的初始值取决于平台。我认为这是一个未定义的行为。
如果您确实需要初始化数组,请改用循环:
for(int i=0; i < arraysize; ++i)
firstCloudForThisClient[i] = true;
您可能需要反转条件以利用默认初始化:
#include <iostream>
using namespace std;
const int arraysize = 10;
const int myIndex = 1; // note this index does not access the first element of arrays
static bool firstTimeOverall = true;
int main()
{
static bool firstCloudForThisClient[arraysize] = {}; // default initialise
cout.flush();
if (!firstCloudForThisClient[myIndex])
{
cout << "I never get here" << endl;
firstCloudForThisClient[myIndex] = true; // Mark used indexes with true
if (firstTimeOverall)
{
firstTimeOverall = false;
cout << "But think I would get here if I got in above" << endl;
}
}
return 0;
}
static bool firstCloudForThisClient[arraysize] = {true};
这会将第一个条目初始化为 true,将所有其他条目初始化为 false。
if (firstCloudForThisClient[myIndex])
但是,由于 myIndex
是 1 并且数组索引是 zero-based,这会访问 second 条目,这是错误的。
您应该访问数组的第一个元素,因此使用:
const int myIndex = 0;