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?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Razielruss
  • 139
  • 1
  • 7
  • 4
    Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman May 10 '20 at 21:51
  • No i have never debugged in code blocks. I just used printf("test") to locate the point of the error. But why there ist an error I literally dont know – Razielruss May 10 '20 at 21:53
  • 2
    Time to dust off and use that debugger. it'll show you precisely what's going on internally before the explosion. `printf`-debugging can only get you so far. – tadman May 10 '20 at 21:54
  • Why use dynamic allocation for such small arrays? This massively complicates your code. This would also be a whole lot less messy if you had an xy pair struct to work with. – tadman May 10 '20 at 21:55
  • You may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Andreas Wenzel May 10 '20 at 21:55
  • time to use *valgrind* – bruno May 10 '20 at 21:56
  • It's very hard to tell what's going on here at a glance. Errors like that are usually caused by out-of-bounds writes, so check your array sizes before writing to them. – tadman May 10 '20 at 21:57
  • One thing that sticks out is your array is *length 2* (`dimOriginal`) and you go ahead and stomp all over memory by writing to the third element! – tadman May 10 '20 at 21:57
  • You forgot `#include `. – Adrian Mole May 10 '20 at 21:57
  • Ok when I use the debugger for which values I have to search. I knot what it is and it would be easy if I had maybe an Index error or something like this but there is an memory error. I never head an error about the memory – Razielruss May 10 '20 at 21:58
  • 1
    if you run your program with *valgrind* you will see for instance you do `xArrayOriginal[2] =2;` which is invalid because you array is only for 2 elements rather than for 3, etc – bruno May 10 '20 at 21:59
  • Also, this line: `y_org[0] = y_int[0];` is using an uninitialized RHS. – Adrian Mole May 10 '20 at 22:00
  • In C you can access any element you want, but if you access elements out-of-bounds you don't necessarily get a crash, you just get *undefined behaviour* which could be a crash, or weird output, or *anything* really. C does not test anything for you, as the programmer you have **complete responsibility** for using your arrays correctly. – tadman May 10 '20 at 22:00
  • god seriously sry guys for this stupid question. I didn't saw it. Ty you it was a dumb error... Time to go to bed – Razielruss May 10 '20 at 22:02
  • `0xC0000005` indicate a protection violation. The most common cause of this is dereferencing a NULL pointer. – ikegami May 10 '20 at 22:05
  • @bruno, Is there a `valgrind` port for Windows? ([No, but...](https://www.deleaker.com/blog/2020/01/04/valgrind-for-windows/)) – ikegami May 10 '20 at 22:05
  • @ikegami Windows ? the OP does not mention WIndows, but if available on it this is a very good news – bruno May 10 '20 at 22:08
  • @bruno, They said they got error `0xC0000005` rather than saying they got a SIGSEGV. That would put them on Windows – ikegami May 10 '20 at 22:12
  • @ikegami as yes you are very probably right (I do not develop under Windows) – bruno May 10 '20 at 22:13

1 Answers1

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.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
bruno
  • 32,421
  • 7
  • 25
  • 37