0

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.

16
  • You never verify whether your memory allocation succeeded. Commented Mar 10, 2020 at 16:36
  • @Gerhardh sorry the exception gets thrown at the first for loop on this line coords[i] = malloc(cols * sizeof(double)); Commented Mar 10, 2020 at 16:41
  • Please post all of your code. It should be compilable and runnable. As it is, there are too many values that are initialized outside of the code you've posted. Any one could have an issue. We can't help you with what we don't know about. You are accessing the array as if you used the first allocation method. To use the commented out one, a different access method would be required. Commented Mar 10, 2020 at 16:42
  • Sorry again the ndivx y and z are all 19 Commented Mar 10, 2020 at 16:42
  • That means that coords is probably a NULL pointer. As I mentioned, you never check. I wonder why it fails. ~7,000*8 = 56kB isn't that large for malloc to fail. Commented Mar 10, 2020 at 16:44

1 Answer 1

1

Aside from some of the other errors [after correction], all values of coords are set to zero. This is because coords is a pointer to int and not (e.g.) double and your equation uses -1.0 / ... which will always produce a fraction.

Also, as David pointed out, you're indexing from 1 [vs. 0] in the for loops. This could cause access violations/segfaults.

I've changed the for loops to start from 0. And, I've adjusted the equation accordingly (using a macro).

You were defining some things like index variables or size variables as double instead of int (e.g.) ndivx

Also, I introduced a typedef for the coordinate values.

Here's some cleaned up code that may help get you further:

#include <stdio.h> #include <stdlib.h> #if 0 double ndivx, ndivy, ndivz, nt, r, box, dx, totnode; #endif #if 0 typedef int coord_t; #else typedef double coord_t; #endif #define SETCOORD(_xidx,_var) \ do { \ coords[numm][_xidx] = -1.0 / 2.0 * r + (dx / 2.0) + (_var * dx); \ printf("coords[%d][%d]=%g\n",numm,_xidx,(double) coords[numm][_xidx]); \ } while (0) int main(void) { #if 1 int ndivx; int ndivy; int ndivz; double r; double dx; #endif ndivx = 19; ndivy = 19; ndivz = 19; int totnode = ndivx * ndivy * ndivz; r = 0.005; // diameter of sphere dx = 0.0025 / ndivx; #if 0 double dx = r / ndivx; // distance between points #else dx = r / ndivx; // distance between points #endif int cols = 3; #if 0 int **coords; #else coord_t **coords; #endif coords = malloc(totnode * sizeof(coord_t *)); for (int i = 0; i < totnode; i++) { coords[i] = malloc(cols * sizeof(coord_t)); } // 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 = 0; i < ndivx; i++) { for (int j = 0; j < ndivy; j++) { for (int k = 0; k < ndivz; k++) { SETCOORD(0,i); SETCOORD(1,j); SETCOORD(2,k); numm = numm + 1; } } } return 0; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Craig I appreciate your time and effort

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.