I'm working on this C program and I've run into an issue with filling a C matrix. Whenever I try to fill the matrix in my update function, I'm hit with a segmentation fault. I don't know if it's an issue with how I'm passing, initializing, or modifying the array.
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int n,m, i, j;
double count = 0;
void update(int n, int m, double **arr, double *arr1){
printf("called update \n");
for(i=0; i<n; i++){
for(j=0; j<m; j++){
count++;
arr[i][j] = count;
arr1[i] = arr[i][j];
}
}
}
int main(int argc, char * argv[]) {
assert(argc==3);
n = atoi(argv[2]);
m = atoi(argv[1]);
double *d_a;
double**arr=(double**)malloc(n*sizeof*arr);
double*arr1=(double*)malloc((n*m)*sizeof*arr1);
double*sum_d1=(double*)malloc(m*sizeof*sum_d1);
for(int i=0; i<n;i++){
arr[i] = (double*)malloc(m*sizeof*(arr[i]));
for(int j=0; j<m; j++){
arr[i][j] = i*m+j;
}
}
for(int i=0; i<n; i++){
for(int j = 0; j<m; j++){
arr1[i*m+j]=arr[i][j];
}
}
update(n,m,arr, arr1);
return 0;
}
Comments
Post a Comment