Exception thrown, Dynamic memory allocation in C

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.

c
exception
memory
dynamic
allocation
asked on Stack Overflow Mar 10, 2020 by kbez • edited Mar 10, 2020 by kbez

1 Answer

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;
}
answered on Stack Overflow Mar 10, 2020 by Craig Estey

User contributions licensed under CC BY-SA 3.0