allocation for global variables in extern
i have a multithreaded program that does the following:
Code:
//global variables
double ***T; //T is the temporary array of matrices
double ***S; //S is the global version of A for the thread function
int *aa; //aa is the global version of a for the thread function
int matrnum; //the number of the matrix currently being calculated
//function called by each thread
void *calc_row(void * t_num)
{
.....
.....
.....
}
double **programY(double ***A, int *a, int mat_num)
{
#ifdef SESC
sesc_init();
#endif
double **result;
int i, j;
T = (double ***) malloc((mat_num)*sizeof(double **));
S = (double ***) malloc((mat_num)*sizeof(double **));
T[0] = A[0];
S=A;
aa=a;
for(i=1; i<mat_num; i++)
{
T[i] = (double **)malloc(a[0]*sizeof(double *));
for(j=0; j<a[0]; j++)
{
T[i][j] = (double *)malloc(a[i+1] * sizeof(double));
}
}
.......
.......
.......
for(i=1; i<mat_num-1;i++)
{
for(j=0; j<a[0]; j++)
{
free(T[i][j]);
}
free(T[i]);
}
free(T);
return result;
}
i'm getting all sorts of weird errors. i'm not sure what more i need. do i need to have a malloc and a free for aa? do i need to do additional allocation or deallocation for S? please help.
this file (programy.c) is actually accessed by a different program (matmult.c) using:
extern double **programY(double ***A, int *a, int mat_num);
i don't know if that makes a difference.
here's a variable list:
T is an array of matrices used for storing results and is accessed in calc_row()
A is an array of matrices
S is a global version of A accessed in calc_row()
a is an array of integers
aa is a global version of a accessed in calc_row()
matrnum is not the same as mat_num, but is incremented in programY() after the threads finish
t_num is the number of the row currently being worked on
mat_num is the number of matrices in A