我的 float 双指针函数在我的 main() 程序中被调用时一直遇到 运行-time 错误

My float double pointer function keeps experiencing run-time error when being called in my main() program

代码说明

我正在尝试创建一个 float 类型的双指针函数,并且有 3 个 float 参数。该函数应该通过使用“malloc”和“calloc”函数为二维数组分配内存来存储微分方程的数值解来求解一阶常微分方程。

求解器的原型函数是 float **RK2(float t0, float x0, float h);,其中 t0x0 是方程的初始条件,h 是使用的步长。

在执行所有计算并将所有数据存储在名为 x 的分配数组中后,我 return 一个指向包含所有内容的数组 x 的双指针的解决方案。

main() 期间,我调用函数 RK2 并将 return 值设置为表示为 p 的双指针,我想将其指向数组 x。调用命令是 p = RK2(t0, x0, h),其中 p 由 float **p = NULL

声明

相关代码

在代码中,下面的函数func就是包含微分方程信息的函数

#include <stdio.h>
#include <stdlib.h>
#define ROWS 1000
#define COLUMNS 2

float func(float x, float t);
float **RK2(float tt0, float x0, float h);
    
int main(){

    float t0 = 0.0;
    float x0 = 1.0;
    float h = 0.001;
    float **p = NULL;

    p = RK2(t0, x0, h);

    return 0;
}

float **RK2(float t0, float x0, float h){

    // Declaration and Initialising
    /* Here included other variables used for calculations*/
    float **x = NULL;

    // Allocating Memory
    x = (float **)calloc(ROWS, sizeof(float *));
    if (x == NULL){
        printf("Out of memory!\n");
        return NULL;
    }
    for (int i=0; i<ROWS; i++){
        x[i] = (float *)calloc(COLUMNS, sizeof(float));
        if (x[i] == NULL){
            printf("Out of memory\n");
            return NULL;
        }
    }

    // Initial Conditions
    x[0][0] = t0, x[1][0]=x0;

    // Calculations and storing data into memory
    for (int i=0; i<ROWS; i++){
        x[0][i] = /* Calculation*/
        /* Intermediate Calculations*/
        x[1][i] = /* Calculation*/
    }        

    return x;
}

问题

然而,当 运行 时,我遇到了某种错误,我已将其归因于 RK2 函数。我通过评论这个函数调用和 main() returned a 0.

进行了测试

此外,我使用 printf 命令在函数内和函数外打印了我的解决方案的值,并且函数内的值是正确的,正如我预期的那样,但是当在函数外打印时函数,值不再匹配。该函数将被正确调用,但当我不得不 return 数组 x 时似乎失败了。但是,双指针 p 确实与函数内创建的二维数组的地址相匹配。但是程序还是没能正确运行。

额外

我的目标是将解决方案信息打印到 main() 函数内的文件中,以通过绘制它和解析解决方案来测试它。当目录中没有生成文件时,我意识到出了点问题。我注释掉了将信息写入文件的代码,但代码仍然没有成功完成 运行ning。

此外,这是我的第一个问题,所以请就如何更好地提出问题或是否需要包含其他任何内容或是否需要删除或更改任何内容提出建议。谢谢 :D.

// i describes the rows
// but you're using i as column descriptor
// c stores multidimensional arrays in row major order
for (int i=0; i<ROWS; i++){
    x[0][i] = // <- change to x[i][0]
    x[1][i] = // <- change to x[i][1]
}