I wish to use a double **** in order to cross an application to dll boundary using a plain old data type. I have done this successfully with double ** and double *** pointers. However, allocating double **** has been a problem.

Here's the code I have developed:

Code:double **** Allocate4(double **** mat, int k, int l, int m, int n) { mat = new double *** [ sizeof(double) * k * l * m * n ]; for(int i = 0; i < k; i++) mat[i] = new double **[ sizeof(double) * l * m * n ]; for(int i = 0; i < l; i++) { for(int j = 0; j < m; j++) { mat[i][j] = new double *[ sizeof(double) * m * n ]; } } for(int i = 0; i < k; i++) { for(int j = 0; j < l; j++) { for(int q = 0; q < m; q++) { mat[i][j][q] = new double [ sizeof(double) * n ]; } } } return mat; }// Allocate4(double **** mat, int k, int l, int m, int n) void DeAllocate4(double **** mat, int k, int l, int m, int n) { for(int i = 0; i < k; i++) { for(int j = 0; j < l; j++) { for(int q = 0; q < m; q++) { delete mat[i][j][q]; mat[i][j][q] = NULL; } } } for(int i = 0; i < l; i++) { for(int j = 0; j < m; j++) { delete mat[i][j]; mat[i][j] = NULL; } } for(int i = 0; i < k; i++) { delete mat[i]; mat[i] = NULL; } delete mat; mat = NULL; }// DeAllocate4(double **** mat, int k, int l, int m, int n)

Attempting to use this code, I find that there are boundary violations with certain combinations of the input parameters, k, l, m, n. For example, etc.

res k l m n

- 3 1 12 12

+ 3 3 12 12

- 2 3 12 12

- 4 3 3 3

- 3 4 3 3

- 4 4 3 3

+ 4 4 4 4

+ 12 12 12 5

+ 12 12 12 6

- 12 12 6 12

- 12 12 6 6

- 11 11 6 6

Using VS 2010 debugger, when crash occurs, typically looks like this:

mat[i][j] = new double *[ sizeof(double) * m * n ];

[-] mat 0x00470068

[-] 0x0060bd40

[-] 0x0047a2e8

[-] 0xcdcdcdcd

(!) CXX0030: Error: expression cannot be evaluated

First-chance exception at 0x0fa3be1f (ctap.dll) in MyApp.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.

Unhandled exception at 0x0fa3be1f (ctap.dll) in MyApp.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.

The program '[0x1BA8] MyApp.exe: Native' has exited with code -1073741819 (0xc0000005).

Here's an example program for illustration:

I believe there is a problem with the quadruple pointer allocator, but I cannot find what it is. Can anyone help me to properly allocate / deallocate such a pointer? Thank you.Code:int main() { int iOrg = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); _CrtSetDbgFlag(iOrg | _CRTDBG_LEAK_CHECK_DF); double **** pfTensor4; int k, l, m, n; k = 10; l = 11; m = 6; n = 6; // error at: mat[i][j] = new double *[ sizeof(double) * m * n ]; pfTensor4 = NULL; pfTensor4 = Allocate4(pfTensor4, k, l, m, n); srand (time(NULL)); for(int i = 0; i < k; i++) { for(int j = 0; j < l; j++) { //printf("Index(%d)\n", i); _RPT2(0, "Index(%d, %d)\n", i, j); for(int q = 0; q < m; q++) { for(int r = 0; r < n; r++) { pfTensor4[i][j][q][r] = (double)rand()/rand(); //printf("%0.5f ", pfTensor3[i][j][q][r]); _RPT1(0, "%0.4f ", pfTensor4[i][j][q][r] ); } _RPT0(0, "\n"); } //printf("\n"); _RPT0(0, "\n"); } } //printf("\n"); _RPT0(0, "\n"); DeAllocate4(pfTensor4, k, l, m, n); #ifdef _DEBUG cout << "Press Enter to continue... "; cin.get(); #endif return 0; }// main()