简体   繁体   English

STD :: COUT影响程序的功能

[英]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; 但是如果我注释掉这行:std :: cout <<可能性<<“可能性” << 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 pastebin: 代码 pastebin: 输入文件

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 . 我相信(不幸的是,无法测试这台计算机上没有C ++编译器)问题出在133行,在此您永远不会给出要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. 如果单独保留此堆栈变量,它将继续为1,并且在以后的第149行中都会通过测试。cout在当前堆栈的顶部创建多个堆栈帧,覆盖内存中的值并更改结果。 Change line 133 to something like 将第133行更改为类似

unsigned int count = 0;

Note that you do also have a count variable in scope already when this is declared; 请注意,在声明该变量时,它的作用域中确实已经有一个count变量。 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. 如果您确实想使用那个,请删除第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. 开头可能是1,这就是这里发生的情况,因为1是以前调用函数时遗留在内存中的。

For posterity, in case pastebin (God forbid) eventually dies, this is the troubling section: 为了后人,万一pastebin(上帝禁止)死了,这是令人不安的部分:

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. 您尚未初始化board变量。 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; 最后,我想指出,按照惯例,c ++中的类应为PascalCase。 this helps clarify that Grid is a constructor, as functions would never start with a capital. 这有助于阐明Grid是构造函数,因为函数永远不会以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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