c++ - 程序仅适用于包含(无副作用)cout 语句?

标签 c++ cout pascals-triangle

所以我一直在研究 problem 15 from the Project Euler's website ,并且我的解决方案运行良好,直到我决定在编写代码时删除我正在使用的 cout 语句 进行调试。我的解决方案通过在一维数组中生成帕斯卡三角形并找到与用户指定的 NxN 晶格中的路径数相对应的元素来工作。这是我的程序:

#include <iostream>
using namespace std;

//Returns sum of first n natural numbers
int sumOfNaturals(const int n)
{
    int sum = 0;
    for (int i = 0; i <= n; i++)
    {
        sum += i;
    }
    return sum;
}

void latticePascal(const int x, const int y, int &size)
{
    int numRows = 0;
    int sum = sumOfNaturals(x + y + 1);
    numRows = x + y + 1;

    //Create array of size (sum of first x + y + 1 natural numbers) to hold all elements in P's T
    unsigned long long *pascalsTriangle = new unsigned long long[sum];
    size = sum;

    //Initialize all elements to 0
    for (int i = 0; i < sum; i++)
    {
        pascalsTriangle[i] = 0;
    }
    //Initialize top of P's T to 1
    pascalsTriangle[0] = 1;
    cout << "row 1:\n" << "pascalsTriangle[0] = " << 1 << "\n\n";  // <--------------------------------------------------------------------------------

    //Iterate once for each row of P's T that is going to be generated
    for (int i = 1; i <= numRows; i++)
    {
        int counter = 0;
        //Initialize end of current row of P's T to 1
        pascalsTriangle[sumOfNaturals(i + 1) - 1] = 1;
        cout << "row " << i + 1 << endl;   // <--------------------------------------------------------------------------------------------------------

        //Iterate once for each element of current row of P's T
        for (int j = sumOfNaturals(i); j < sumOfNaturals(i + 1); j++)
        {
            //Current element of P's T is not one of the row's ending 1s
            if (j != sumOfNaturals(i) && j != (sumOfNaturals(i + 1)) - 1)
            {
                pascalsTriangle[j] = pascalsTriangle[sumOfNaturals(i - 1) + counter] + pascalsTriangle[sumOfNaturals(i - 1) + counter + 1];
                cout << "pascalsTriangle[" << j << "] = " << pascalsTriangle[j] << '\n';   // <--------------------------------------------------------
                counter++;
            }
            //Current element of P's T is one of the row's ending 1s
            else
            {
                pascalsTriangle[j] = 1;
                cout << "pascalsTriangle[" << j << "] = " << pascalsTriangle[j] << '\n';  // <---------------------------------------------------------
            }
        }
        cout << endl;
    }

    cout << "Number of SE paths in a " << x << "x" << y << " lattice: " << pascalsTriangle[sumOfNaturals(x + y) + (((sumOfNaturals(x + y + 1) - 1) - sumOfNaturals(x + y)) / 2)] << endl;
    delete[] pascalsTriangle;
    return;
}

int main()
{
    int size = 0, dim1 = 0, dim2 = 0;

    cout << "Enter dimension 1 for lattice grid: ";
    cin >> dim1;
    cout << "Enter dimension 2 for lattice grid: ";
    cin >> dim2;
    latticePascal(dim1, dim2, size);

    return 0;
}

似乎在保存我的程序的 cout 语句标有注释箭头。只要包含这些行中的任何,它似乎就可以工作。 如果删除所有这些语句,则程序将打印:“Number of SE paths in a”然后挂起几秒钟,然后终止没有打印答案。我希望这个程序尽可能干净并简单地输出答案而不必打印三角形的全部内容,因此它在当前状态下无法按预期工作。

最佳答案

计算数组索引的表达式或计算分配数组大小的表达式很有可能会导致未定义的行为,例如堆栈溢出。

因为未定义此未定义行为对您的可见性,所以程序可以按您的预期运行,或者它可以做其他事情 - 这可以解释为什么它适用于一个编译器而不适用于另一个编译器。

您可以使用带有 vector::resize() 和 vector::at() 的 vector 而不是带有 new 和 [] 的数组,以在程序在写入或刷新其所有内容之前中止的情况下获得一些改进的信息由于无效的内存访问而输出。

如果问题是由于使用了无效的索引,那么 vector::at() 将引发一个你不会捕获的异常,许多调试器在发现这对因素时会停止,它们会帮助你检查程序中发生问题的点和关键事实,例如您尝试访问的索引和变量的内容。

它们通常会向您显示比您预期更多的“堆栈帧”,但有些是系统如何管理未捕获异常的内部细节,您应该期望调试器帮助您找到与您的问题相关的堆栈帧,以便您可以检查那个的上下文。

关于c++ - 程序仅适用于包含(无副作用)cout 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682106/

相关文章:

c++ - 在 win32 中连续串行写入,C++

python - 使用 Code::Blocks 构建 OpenCV.cbp 时出现 Python.h 问题

c++ - 换行符改变了从文件中读取的字符串在 C++ 中的打印方式

C++ 在最后一个循环中排除部分循环运算符

c++ - 在第 1500 行找到帕斯卡三角形中的每个数字?

c++ - Linux 上 C/C++ 中的简单原始套接字服务器

c++ - 为什么 std::exception 在 VC++ 中有额外的构造函数?

带前缀的 C++ cout

algorithm - 如何有效地计算帕斯卡三角形中的一行?

c++ - 在 C++ 中居中 Pascal 的三角形输出