Howdy
I have the following code:
int nprocs;
int ByteNum, Allocation, startpoint;
struct stat num;
FILE *file;
int *Numbers;
int **ProcSort;
int *ProcAlloc;
int main()
{
nprocs = mp_numthreads();
startpoint = 0;
stat("rnums2m", &num);
ByteNum = num.st_size;
Allocation = (ByteNum/nprocs)/sizeof(in
t);
Numbers = (int *)malloc(ByteNum);
ProcSort = (int **)malloc(nprocs * sizeof(int *));
ProcAlloc = (int *)malloc(nprocs * sizeof(int));
file = fopen("rnums2m", "r");
fread(Numbers, sizeof(int), ByteNum/sizeof(int), file);
fclose(file);
for (a=0;a<nprocs;a++)
{
ProcSort[a] = (int *)malloc(ProcAlloc[a]);
for (b=0;b<ProcAlloc[a];b++)
{
ProcSort[a][b] = Numbers[b+startpoint];
}
startpoint = startpoint + ProcAlloc[a];
printf("Here! %d\n",a);
}
Copius printf statements have led me to the conclusion that the seg fault is occurring in the nested FOR loop after I've malloc'd the second dimension of the array to the size of the first allocation of the number of bytes in the file. Basically, I'm trying to take the numbers from the Numbers array, split them into two dimensional arrays, and flip each of those arrays to however many processors the program has available. So,
ProcSort[0][500,889] would send that hunk of numbers to processor 0
ProcSort[1][500,889] " " 1
etc.
The seg fault, then, is occurring in the second FOR loop, but why? I'm a long-time Java guy and C is laughing at my incompetence. Please help.
Jason
Start Free Trial