1

Why are my array of static bools not initialized properly? Only the first one is initialized - I suspect this is because the array is static.

The following MWE was compiled with GCC and is based on a function that I am writing that I have transferred into a main program to illustrate my problem. I have tried with and without c++11. My understanding is because this array is static and initialized to true this should always print the first time I enter my function. So in this MWE it should print once.

#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;
}
1
  • 1
    What makes you think that all the elements of firstCloudForThisClient are initialised to true? Commented Feb 20, 2017 at 10:16

4 Answers 4

1

You may need to invert your conditions to take advantage of default initialisation:

#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;
}
Sign up to request clarification or add additional context in comments.

Comments

1
static bool firstCloudForThisClient[arraysize] = {true};

This initializes the first entry to true, and all others to false.

if (firstCloudForThisClient[myIndex])

However, since myIndex is 1 and array indexing is zero-based, this accesses the second entry, which is false.

Comments

0

Your are initializing only first element on an array using array[size] = {true} , if arraysize variable is bigger then 1, the initial value of other elements depends on platform. I think it is an undefined behavior.

If you really need to init your array, use loop instead:

for(int i=0; i < arraysize; ++i)
firstCloudForThisClient[i] = true;

8 Comments

Because this is a static variable for use in a function that is called multiple times I cannot use a loop - does this mean that I should turn this function into an object?
Yes, it could be usefull to turn this functionality to an object. Or you can move this loop into other function and call it once, for example in the beginning of the program. If you like static varaible so much, you even create a static bool flag, that would protect you of initing array again.
Thank you for your suggestion - I am playinf trying to get some more complicated threading code working with functionality from the point cloud library. For this reason I will take your latter suggestion for now and take your former suggestion (turning it into an object) later. Thank you!
@GoverNator A list initializer on an array uses the given initializers in order, and value-initializes the rest of the entries, meaning 0 for all primitives (i.e. false for bools). It is not undefined, and not dependent on the platform.
@user3235290 Because the answer is incorrect, as I explained in my comment above.
|
0

You should access the first element of the array so use:

const int myIndex = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.