Advertisement

09.24.2007 at 11:02AM PDT, ID: 22849023
[x]
Attachment Details

C Multi-Dimensional Array Memory Allocation

Asked by cdromes in C Programming Language, Linux Programming

Tags: array, c, dimensional, allocation, memory

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(int);

      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
 
Loading Advertisement...
 
[+][-]09.24.2007 at 11:21AM PDT, ID: 19950421

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 11:30AM PDT, ID: 19950500

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 02:51PM PDT, ID: 19952059

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 04:36PM PDT, ID: 19952487

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, Linux Programming
Tags: array, c, dimensional, allocation, memory
Sign Up Now!
Solution Provided By: Axter
Participating Experts: 2
Solution Grade: A
 
 
[+][-]09.25.2007 at 05:00AM PDT, ID: 19954923

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.25.2007 at 06:40AM PDT, ID: 19955587

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628