I am trying to allocate memory for a structure that looks like this.

Code:
   typedef struct
   {
      int nID;
      char ** pMessage;
   } MyStruct;

   MyStruct ** pSections[10];
   int str[] = {0,1,10,88,1000,534,....};

   for (i = 0; i < 10; i++)
   {
      pSections[i] = (MyStruct **) calloc(1, sizeof(MyStruct *)*str[i]);
   }

   for (i = 0; i<10; i++)
   {
        for (j = 0; j<str[i]; j++)
       {
              pSections[i][j] = ... // Some previously allocated MyStruct address allocated elsewhere using calloc(1, sizeof(MyStruct))
       }
   }
As you can see, one dimension of the 3D array pSections, is already allocated at start up. Then the second dimension is allocated in the first for loop. As you can see the sizes of the arrays in the second dimension are not the same. I get an application crash when I assign pSections[i][j]. Not immediately though, I get the crash in calloc after I allocate say 50-60 MyStruct values (BTW, I allocate MyStruct also using calloc)

What I am trying to do -->
  • There are N sections that are defined in a #define SECTIONS 7
  • There are X entries in each section. And each section has a differnt number of entries.
  • I allocate each array in a section using calloc.
  • I allocate each entry using calloc.
  • I Then assign each entry to the array.
  • I then try to access it with pSections[i][j]


Any help is appreciated ...