I'm trying to dynamically allocate a 2D array within a function. I have the code working perfectly if I do the allocation in main. It compiles fine within the function but when I run the code I get the error "program has exited due to signal 10 (SIGBUS)" when i try to run it through xcode and I get "bus error" when I compile it through gcc and try to run it.
Code:
void CreateData(int numDataPts, int numClusters, int Length_data, Node** head, double*** centers)
{
int i,j;
//Dynamically Create 2D arrays to hold Cluster Centers
*centers = (double**)malloc(sizeof(double)*numClusters);
for(i=0; i<numClusters; i++)
*centers[i] = (double*)malloc(sizeof(double)*Length_data); //This line is where the error occurs
//Randomly initialize centers
for(i=0; i<numClusters; i++)
for(j=0; j<Length_data; j++)
*centers[i][j]=decRand()*2-1;
}
note: centers is declared as and the function call is
Code:
CreateData(numDataPts,numClusters,Length_data,&head,¢ers);
.