Search code examples
cfunctionpointersmathallocation

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


Code Description

I'm trying to create a double pointer function with a float type, and have 3 float arguments. This function is supposed to solve a first order ordinary differential equation by allocating memory for a 2D array to store the numeric solutions of the differential equations using the "malloc" and "calloc" functions.

The prototype function for the solver is float **RK2(float t0, float x0, float h); where t0 and x0 are initial conditions for the equation and h is the stepsize being used.

After performing all of the calculations and storing all of the data within the allocated array named x, I return a double pointer which points to the array x which contains all of the solutions.

During my main(), I call the function RK2 and set the return value to a double pointer denoted p which I want to point to the array x. The call command was p = RK2(t0, x0, h) where p was declared by float **p = NULL

Relevant Code

Within the code, the function func below simply is the function which contains the information of the differential equation.

#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;
}

Problem

However when ran, I'm experiencing some sort of error which I have pinned down to being due to the RK2 function. I tested by commenting this function call out and the main() returned a 0.

Also, I printed the values of my solutions both within the function and outside of the function using the printf command and the values within the function were correct and as I expected, but when being printed outside of the function, the values no longer matched. The function would be called correctly, but seemed to fail when I had to return the array x. However, the double pointer p did match the address of the 2D array created within the function. But the program still failed to run correctly.

Extra

I'm aiming to also print the solution information onto a file within the main() function to test it by plotting it and the analytical solution. I realised something went wrong when no file was produced in the directory. I commented out the code writing the information onto the file and code still didn't complete running successfully.

Also, this is my first ever question, so please give me advice on how to ask a question better or if I need to include anything else or if I need to remove anything or change anything. Thanks :D.


Solution

  • // 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]
    }