1

I have a question about why something is being done in the provided stub code for Tideman.

here bool locked[MAX][MAX] is left uninitialized early in the program when it is declared. Later every element of locked is assigned false in main using for loops.

I have learned the default value of booleans is false, so it seems to me locked doesn't need to be assigned false explicitly. Would you teach me for what purpose locked is assigned false in this program?

/// some code, it defines MAX 

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

/// more code introduces `candidate_count`

int main(int argc, string argv[])
{
    /// some code here
    /// notably it assigns `candidate_count` to a value less than `MAX`

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    /// more code follows
}
1
  • I have trimmed down the question to a minimalized form, that focuses on asking your question and removes inconsequential details that are not relevant. Your question really has nothing to do with Tideman, and has nothing to do with the rest of the code. Your question is simply why are they doing this loop.
    – UpAndAdam
    Jan 29 at 16:38

1 Answer 1

1

There is no default value initialization guarantee of any kind in C.

I don't know where you learned the default value of booleans is false but I strongly question that information and disagree with it. Yes some compilers will do that for you, and yes in debug mode even more will; but that is not reliable at all. You should always initialize all your variables before using them. You will receive compiler warnings if you do not, and you will experience a world of pain and hurt in the form of non-deterministic behavior (in layman's terms inconsistent behavior) causing sporadic failures and causing latent bugs to be missed and will make your life a pain.

Non-Determinism is the cause of many of the hardest bugs to figure out and debug because it ruins reproducibility. Things don't happen consistently or reliably; it might work 99 times out of 100, but that 1 time it doesnt work right you are screwed. And the fact that it could be that rare ( or rarer) makes it harder to find, and hard to reproduce.

In layman's terms, non-determinism means that the same inputs don't always lead to the same outputs, etc. In most math you have or will encounter things are deterministic, the same input always has the same output. (Yes there are parts of math/physics that deal with this not being true but if you know about them then you don't need this explanation (: ) Example: two plus two always equals four, area of a circle is two time pi times radius, for some function f(x) = y, for all values of x, f(x) = f(x), etc.

Non-determinism would be like if you had a function in math f(x) = 3x + 2 + q() where q() returns 0 99% of the time and a random number the other 2%. As a result sometimes, and randomly, f(x) can just return an unexpected value. Clearly it should be evident that if you had a composition of functions like: (assume a, b, c, d, e are all functions. )
result = a(b(f(x))) + c( d(f(x)), e(f(x)) )
You are in for a world of pain figuring out what's wrong when it doesn't give you the proper result.

That's basically what you have with programming.

I hope that helps not just answer the question, but explain some deeper background about it and Computer Science / Programming in general. Best of luck with your adventures in coding.

2
  • Thank you very much for your help, UpAndAdam! It helped a lot!
    – Yuya Ito
    Jan 31 at 4:47
  • Very happy to help @YuyaIto It is a fair question and a good question even though it seems obvious to many.
    – UpAndAdam
    Jan 31 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .