简体   繁体   中英

STD::COUT affecting functionality of program

So I am in the middle of wring a function for a much larger project. Its messy at the moment, but I am experiencing some very strange behaviour.

The portion of my code where the behaviour originates is shown below:

while(rw < (startY + 3)) {
        while(cl < (startX + 3)) {
            if(board[rw][cl] == '*'){ 

                char poss[9] = {'1','2','3','4','5','6','7','8','9'};
                unsigned int totalcount = 0;
                unsigned int possibilities = 0;
                unsigned int possibleRow = 0;
                unsigned int possibleCol = 0;
                unsigned int lastCheck = 0;

                for(unsigned int alpha = 0; alpha < 9; alpha++){
                    if (testGrid('r', poss[alpha], rw) == true) { totalcount++; }
                    if (testGrid('c', poss[alpha], cl) == true) { totalcount++; }
                    if (testGrid('s', poss[alpha], 0) == true) { totalcount++; }
                    if(totalcount == 0) { possibilities++; }
                    totalcount = 0;
                }

                std::cout << possibilities << " possibilities" << std::endl;

                if(possibilities == 1) {
                    possibleRow = rw;
                    possibleCol = cl;

                    for(unsigned int alpha = 0; alpha < 9; alpha++){
                        if (testGrid('r', poss[alpha], possibleRow) == true) { lastCheck++; }
                        if (testGrid('c', poss[alpha], possibleCol) == true) { lastCheck++; }
                        if (testGrid('s', poss[alpha], 0) == true) { lastCheck++; }
                        if(lastCheck == 0) { board[rw][cl] = poss[alpha]; }
                        lastCheck = 0;
                    }
                }
                possibilities = 0;
            }
            cl++;
        }
        rw++;
        cl = startX;
    }   

The output of the entire program solves one small square of a sudoku grid (infant stages). But if i comment out the line: std::cout << possibilities << " possibilities" << std::endl; the output is different (other than the obvious lack of output. shown below:

在此处输入图片说明

Obvious this is undesired behaviour. But can anyone explain it?

pastebin: the code pastebin: the input file

I believe (can't test, unfortunately, have no C++ compiler on this machine) that the issue is coming from line 133, where you never give a value to count . If this stack variable is left alone, it will continue to be 1, and the test will pass every subsequent time on line 149. The cout creates several stackframes on top of the current stack, overwriting that value in memory and changing your results. Change line 133 to something like

unsigned int count = 0;

Note that you do also have a count variable in scope already when this is declared; perfectly legal, but I just want to point it out in case the intent was to be using that one, and not making a new one. If you do want to use that one instead, remove line 133.

Declaring a primitive and using it when you may not have given it a value is a recipe for odd behavior. You have no idea what's in the memory given to that variable, so its value could theoretically be completely arbitrary. It's possible it could be 1 to begin with, which is what's happening here, since the 1 was left over in memory from previous calls to the function.

For posterity, in case pastebin (God forbid) eventually dies, this is the troubling section:

unsigned int startY = 0;
unsigned int startX = 0;
unsigned int count; //line 133

startY = (num / 3) * 3;
startX = (num % 3) * 3;

unsigned int rw = startY;
unsigned int cl = startX;

while(rw < (startY + 3)) {
    while(cl < (startX + 3)) {
        if(board[rw][cl] == ident){ count = 1; }
        cl++;
    }
    rw++;
    cl = startX;
}
if(count == 1){ //line 149
    return true;
} else {
    return false;
}

You have not initialized your board variable. Depending on your compiler version, you should change the line defining it to

board[9][9] = { {0} };

But, more likely, you will have to create a constructor as follows

grid() : board{ {0} } { */empty constructor*/};

Finally, I would like to point out that classes in c++ should be PascalCase by convention; this helps clarify that Grid is a constructor, as functions would never start with a capital.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM