You are allocating an 1-dimensional array with
nRows
*
nColumns
elements of pointers to
float
and casting this to a
float**
. This will not work (the total array size would match for floats in this case because sizeof(float) == sizeof(float *) for 32-bit builds, but accessing 2-dim members will cause access violations).
To fix the problem, you have two options:
1. Use a 1-dimensional array and calculate the index when accessing elements:
stpData->dbpMG1G1_data = (float *)malloc(sizeof(float) * nRows * nColumns);
stpData ->dbpMG1G1_data[nRow * nColumns + nColumn] = fValue;
1. Use a 2-dimensional array which must be allocated using a loop:
stpData->dbpMG1G1_data = (float **)malloc(sizeof(float *) * nRows);
int i;
for (i = 0; i < nRows; i++)
stpData ->dbpMG1G1_data[i] = (float *)malloc(sizeof(float) * nColumns);
The first version is more performat and does not require freeing the memory inside another loop.