In C returned error: -1073741819 (0xC0000005)

1

I program in C and I am testing my program and I don't understand why I get the error -1073741819 (0xC0000005). Here is the Code:

#include <stdio.h>
#include <stdlib.h>

double interpol(double x0, double y0, double x1, double y1, double h) {
    double ergebnis = (y1 - y0) / (x1 - x0) * h;
    printf("%.2f\n", ergebnis);
    return ergebnis;
}

void interpol_array(double *x_org, double *y_org, int dim_org,
                    double *x_int, double *y_int, int dim_int,
                    int n) {
    int j, i;
    double h;

    i = 0;
    y_org[0] = y_int[0];
    for (j = 1; j < dim_int; j++) {
        if (j % (n + 1) == 0 && j != 0) {
            i++;
            x_int[j] = x_org[i];
            y_int[j] = x_org[i];
            continue;
        }
        printf("%.2f ", y_org[0]);
    }
}

int main() {
    /* Pointer erzeugen auf null setzen */
    double *xArrayOriginal = NULL;
    double *yArrayOriginal = NULL;

    double *xArrayInterpol = NULL;
    double *yArrayInterpol = NULL;

    /**/
    int dimOriginal = 2;
    int n = 3;
    int dimInterpol = (n + 1) * (dimOriginal + 1) - n; /*+1 wegen der null*/

    int i, j;
    double h;

    /* Array für die Originalwerte erzeugen */
    xArrayOriginal = (double *)malloc(dimOriginal * sizeof(double));
    yArrayOriginal = (double *)malloc(dimOriginal * sizeof(double));

    /*Array für das Interpolierte Array erzeugen*/
    xArrayInterpol = (double *)malloc(dimInterpol * sizeof(double));
    yArrayInterpol = (double *)malloc(dimInterpol * sizeof(double));

    xArrayOriginal[0] = 0;
    xArrayOriginal[1] = 1;
    xArrayOriginal[2] = 2;

    yArrayOriginal[0] = 2;
    yArrayOriginal[1] = 4;
    yArrayOriginal[2] = 8;

    interpol_array(xArrayOriginal, yArrayOriginal, dimOriginal, xArrayInterpol,
                   yArrayInterpol, dimInterpol, n);
    return 0;
}

In my program I have 4 dynamic arrays. 2 dynamic arrays for origin x and y values and 2 dynamic arrays for interpolated x and y values. I don't program the full interpolation because I got an error. Therefore I searched the error and found out that when I use printf("%.2f ", y_org[0]); I get the error that you can see above. Only when I change it to printf("test"); it works fine. So why do I get this error. what is wrong with my array list?

c
memory
malloc
dynamic-arrays
asked on Stack Overflow May 10, 2020 by Reazelruss • edited May 10, 2020 by chqrlie

1 Answer

2

In the function main, the lines

int dimOriginal = 2;
...
xArrayOriginal =(double *)malloc(dimOriginal*sizeof(double));
yArrayOriginal =(double *)malloc(dimOriginal*sizeof(double));

allocate arrays for 2 double elements each. However, the lines

xArrayOriginal[2] =2;
...
yArrayOriginal[2] =8;

write out of bounds of these arrays, as the only valid elements numbers are 0 and 1. This causes undefined behavior.

Also, in the function interpol_array, the following line causes undefined behavior:

 y_org[0] = y_int[0];

This is because y_int has been assigned the value of yArrayInterpol in main, but the contents of yArrayInterpol has not been initialized.

answered on Stack Overflow May 10, 2020 by bruno • edited May 11, 2020 by Andreas Wenzel

User contributions licensed under CC BY-SA 3.0