C++;程序在随机位置无原因停止 我正在研究一个C++程序,它应该把火焰的二维图像转换成三维模型。该程序主要处理多个矩阵运算,我都是使用指针实现的(我知道,我可以使用向量)。 在输入文本文件、镜像和平滑数据值之后,会对图像的每一行进行校正计算。在该计算的函数开始时,程序在一个随机位置停止,但在for循环中声明y_值向量

C++;程序在随机位置无原因停止 我正在研究一个C++程序,它应该把火焰的二维图像转换成三维模型。该程序主要处理多个矩阵运算,我都是使用指针实现的(我知道,我可以使用向量)。 在输入文本文件、镜像和平滑数据值之后,会对图像的每一行进行校正计算。在该计算的函数开始时,程序在一个随机位置停止,但在for循环中声明y_值向量,c++,pointers,matrix,C++,Pointers,Matrix,以下是代码片段: void CorrectionCalculation(Matrix Matrix_To_Calculate, int n_values, int polynomial_degree, int n_rows) { for (int h = 0; h < n_rows; h++) { //Initialising and declaration of the y_values-vector, which is the copy of each

以下是代码片段:

void CorrectionCalculation(Matrix Matrix_To_Calculate, int n_values, int polynomial_degree, int n_rows)
{
    for (int h = 0; h < n_rows; h++)
    {
        //Initialising and declaration of the y_values-vector, which is the copy of each matrix-line. This line is used for the correction-calculation.
        double* y_values = new double(n_values);
        for (int i = 0; i < n_values; i++)
        {
            y_values[i] = Matrix_To_Calculate[h][i];
        }

        //Initialisiing and declaration of the x-values (from 0 to Spiegelachse with stepwidth 1, because of the single Pixels)
        double* x_values = new double(n_values);
        for (int i = 0; i < n_values; i++)
        {
            x_values[i] = i;
        }
void CorrectionCalculation(矩阵到计算,int n值,int多项式度,int n行)
{
对于(int h=0;h

当计算一行时,程序运行良好。但是当我添加一些代码来计算整个图像时,程序停止。

您分配的不是一个值数组,而是一个元素。 而不是:

double* y_values = new double(n_values);
// ...
double* x_values = new double(n_values);
换成

double* y_values = new double[n_values];
//...
double* x_values = new double[n_values];
您应该使用双精度数组,而不是新数组。这样,当不再需要时,将自动删除内存。例如:

#include <vector>
std::vector<double> y_values(y_values);
#包括
std::向量y_值(y_值);

您还可以使用与参数相同的变量名。这可能会导致代码中的混乱和细微错误,因为您不太确定要更改哪个变量。

double*y\u values=new double(n\u values);
这不会生成数组,而是一个双元素。经过此更正后,程序的行为如何(有两个这样的)“没有理由”。是的,你的程序是完美的,C++设计者是什么样的。你是对的,我应该写“没有明显的理由(对我来说,现在)”。YyValuy和XyValk的初始化是我忘记使用[]而不是()的唯一的事情。。很恼人的是,我没有找出那个错误,但我似乎站在管道上。似乎这是程序在此位置停止工作的错误…很恼人;)。非常感谢。我还必须在初始化x_值时编写此格式。不过,它仍然停止…现在正试图找出错误。“考虑使用向量"除非你有经验,而且真正知道你在做什么,所以没有理由使用数组。我不记得在C++编程十年中合法使用它。好的,我会使用向量。我只是使用数组,因为我们一直认为这样做——我认为主要是因为理解C++。r支持。