0

I've been doing a project on solving differential equations and made a working one on C++ when i was told to make it on C I used function "new" to create dynamic arrays and needed to change it to malloc, so i did :)

But i'm getting errors 255 and -1073741819 randomly

Can you please help me to find an error in my code?

  double **k=(double **)malloc(7 *sizeof(double*));
for(i=0; i<7; i++) k[i]=(double *)malloc(4 *sizeof(double));

double **b=(double **)malloc(7 *sizeof(double*));
for(i=0; i<7; i++) b[i]=(double *)malloc(6 *sizeof(double));

double *mn=(double *)malloc(7 *sizeof(double));

double *yy1=(double *)malloc(4 * sizeof(double));
double *yy2=(double *)malloc(4 * sizeof(double));
int *go;
go=1;

I can't understand if am i using malloc incorrectly?

thanks for your response!

  • 2
    You set a pointer's value to `1`, which seems highly suspicious. – cadaniluk Oct 17 '15 at 21:33
  • Do not cast the result of `malloc` & friends in C! – too honest for this site Oct 17 '15 at 21:33
  • 1
    Runtime error 0xC0000005 in Windows is an "Access Violation". It's something you can expect if you set `int *go = 1`, then try to access "*go"* ;) – paulsm4 Oct 17 '15 at 21:38
  • Apart from the redundant casts on the return value of `malloc`, the general idea for allocating the 1D and 2D arrays seems sound. But as already mentioned above, setting the `int *go` to 1 may be setting the stage for a later disaster. Can you show us what you do later with `go`? – WhiteViking Oct 17 '15 at 21:38
  • well, i was suspicious about this particular moment i am using it in function 'double newh(double h, double y1, double y2, double y3, double y4, double *yy1, double *yy2, int *go, double **k, double *mn)' which returns the value of a new step for my approximation and im changing this '*go' depending on if my new step lesser than an eps (allowed error) thank you for your answers! – zaitsevmishka Oct 17 '15 at 21:39
  • [sigh] when you stepped through the code with your debugger, which line/s generated the faults? – Martin James Oct 17 '15 at 21:43
  • This sounds like a problem that you would run into if you forget to `#include `. – R Sahu Oct 17 '15 at 21:44
  • I have included @MartinJames well, i'm feeling very noobish, but i have no errors or warnings in my debugger – zaitsevmishka Oct 17 '15 at 21:49
  • can i access my arrays like i did when was using new ? – zaitsevmishka Oct 17 '15 at 21:53
  • i've found a solution :) thank you all for the answers! – zaitsevmishka Oct 17 '15 at 22:00
  • it seams that a part of the code is missing. the only suspicious one is trying to set a value of 1 to a pointer. this is not an error, but the compiler will refuse to compile and throw an error. how did you compiled that? – milevyo Oct 17 '15 at 22:03
  • [Don't cast the result of malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Oct 18 '15 at 01:17

1 Answers1

0

As they pointed in the comments, you can't set an int value directly to a pointer since that would lead to a segmentation fault.

The proper way to do it is:

int * go;
int temp = 1;

go = &temp;

Now go points to the memory address of temp, thus the value of *go is 1.