2

How can I work around the Visual Studio Visual C #C2148 error? Here is the code that produces the error:

#define ACOUNT 2000
#define BCOUNT 9000
#define CCOUNT 195

struct s_ptx {

      int pvCount[ACOUNT][BCOUNT][CCOUNT];
} ;

This produces a VStudio 2010 Visual C (compiling under 64bit) error #C2148: error C2148: total size of array must not exceed 0x7fffffff bytes

I know I could dynamically allocate the pvCount 3d array, but then I'd have to do a zillion alloc's and free's. I have 192 gig of memory, so I'm trying to find a compiler switch or option that allows something of this size.

Edit: The complicating issue I left out trying to simplify things is that ptx is a pointer that at runtime is used as an array of structs:

ptx *Ptx        =  (ptx *)   calloc(10,  sizeof(ptx));
for (int i = 0; i < 10; ++i)
    {
     Ptx->pv = (int (*)[BCOUNT][CCOUNT] )  malloc( (unsigned long) ACOUNT * BCOUNT *CCOUNT * sizeof(int));

}


 for (int jav = 0; jav < 10; ++jav)
        for (int j = 0; j < ACOUNT; ++j)
            for (int k = 0; k < BCOUNT; ++k)
                for (int m = 0; m < CCOUNT; ++m)
                    Ptx[jav].pv[j][k][m] = j + k + m;

So when I run the code I get an access violation error, presumably because by doing the dynamic allocation I am no longer able to use: Ptx[jav].pv[j][k][m]

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57

1 Answers1

5

You don't need zillion mallocs. just:

int (*arr)[BCOUNT][CCOUNT]=malloc((size_t)ACOUNT*BCOUNT*CCOUNT*sizeof int);

Edit: the cast to size_t is necessary, to not overflow the int.

asaelr
  • 5,438
  • 1
  • 16
  • 22