Hi I'm trying to do dynamic memory allocation of a large matrix in C but I'm running into the following error:
Exception thrown at 0x00007FF63A248571 in cdempd.exe: 0xC0000005: Access violation writing location 0x0000000000000000. occurred
sometimes it's Access violation writing location 0xFFFFFFFFB412E2A0.
double ndivx, ndivy, ndivz, nt, r, box, dx, totnode; int main() { ndivx = 19.0; ndivy = 19.0; ndivz = 19.0; int totnode = ndivx * ndivy * ndivz; r = 0.005; //diameter of sphere dx = 0.0025 / ndivx; double dx = r / ndivx; // distance between points int cols = 3; int** coords; coords = malloc(totnode * sizeof(int*)); for (int i = 0; i < totnode; i++) { coords[i] = malloc(cols * sizeof(int)); } //int* coord = (int*)malloc(totnode * cols * sizeof(int)); // int offset = i * cols + j; // now mat[offset] corresponds to m(i, j) //create a cube of equidistant points int numm = 0; for (int i = 1; i <= ndivx; i++) { for (int j = 1; j <= ndivy; j++) { for (int k = 1; k <= ndivz; k++) { coords[numm][0] = -1.0 / 2.0 * (r)+(dx / 2.0) + (i - 1.0) * dx; coords[numm][1] = -1.0 / 2.0 * (r)+(dx / 2.0) + (j - 1.0) * dx; coords[numm][2] = -1.0 / 2.0 * (r)+(dx / 2.0) + (k - 1.0) * dx; numm = numm + 1; } } } } pd.r is a double 0.005, dx is a double about 0.00026315, totnode is 6859.
I've tried two methods, the one that is there and the one commented out with //. Both give me the same error. I'm using visual studio 2019. I'm not so familiar with c and visual studio so forgive me if the question is silly. Any help would be appreciated thank you.
coordsis probably aNULLpointer. As I mentioned, you never check. I wonder why it fails. ~7,000*8 = 56kB isn't that large formallocto fail.