Size of multidimentional array

Hi there!. I am performing some calculations with matrices but it happens that I have a lack of RAM memory.
I know how to calculate the memory allocated for a unidimensional array but not higher order. Can anyone provide me a code to know how many bytes have I allocated (as a function of N) for this multidimensional array? You can look the code below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void)
{
int N=1600;

double ****L;
L=new double ***[N+1];
	for(int i=0;i<N+1;i++)
	{
		L[i]=new double **[N+1];
		for(int j=0;j<N+1;j++)
		{
			L[i][j]=new double *[7];
			for(int k=0;k<7;k++)
			{
				L[i][j][k]= new double [7];
			}
		}
	}

return (0);
}


Thanks in advance!
Just think about it this way:

a double[5] = 5x size of a double
a double[5][10] = 10x size of a double[5]
a double[5][10][15] = 15x size of a double[5][10]
and so on, and so on.

In this example, you have a (N+1)*(N+1)*7*7 = ~126mil doubles = ~ 1bil* bytes, which is about a GB.

(*Assuming 8byte doubles, which is most likely the case.)
Good explanation!

My system monitor tells me the application occupy about 1.2 GB which is what you said.
So simply multiply the total number of elements times the number of bytes a double occupies (yes, it is 8 bytes in my case).

I was asking for a code, but I'm satisfied with your explanation.

Thank you again!
Let count together. Assume that pointer occupies 4 bytes on a 32-bit platform. And size of double is equal to 8 bytes. So we have

L=new double ***[N+1]; ===> ( N+1 ) * 4

plus

for(int i=0;i<N+1;i++)
{
L[i]=new double **[N+1]; ===> ( N+1 ) * ( N + 1 ) * 4

plus

for(int j=0;j<N+1;j++)
{
L[i][j]=new double *[7]; ===> ( N + 1 ) * ( N + 1 ) * 7 * 4

plus

for(int k=0;k<7;k++)
{
L[i][j][k]= new double [7]; ===> ( N + 1 ) * ( N + 1 ) * 7 *7 * 8

So the result is

( N + 1 ) * 4 + ( N + 1 ) * ( N + 1 ) * 4 + ( N + 1 ) * ( N + 1 ) * 7 * 4 + ( N + 1 ) * ( N + 1 ) * 7 *7 * 8 = ( N + 1 ) * 4 + ( N + 1 ) ^2 * 4 + ( N + 1 ) ^2 * 7 * 4 + ( N + 1 ) ^ 2 * 7 * 7 * 8 =
4 * ( N + 1 ) + ( 4 + 28 + 392 ) * ( N + 1 ) ^2 = 4 * ( N + 1 ) + 424 * ( N + 1 ) ^ 2 :)

Hi

Under Windows you can get the total avaliable memory by
1
2
3
4
5
6
 MEMORYSTATUSEX status;
    status.dwLength = sizeof(status);
    GlobalMemoryStatusEx(&status);

    long double toal_av_mem = status.ullAvailPhys);


if you are a linux user than you may get the total avaible momery by

1
2
3
    long double pages = (long double)sysconf(_SC_AVPHYS_PAGES);
    long double page_size = (long double)sysconf(_SC_PAGE_SIZE);
    long double total_av_mem = pages * page_size;


than you can compute the require memory and compare it with the avaible memory, so if you have enough memry do other stuf :-)

some matrix libraries do that process, you may have a look at Blizt or Armadillo or MTL or Hasem and see how they do the job :-)

For linux you need to include #include <unistd.h> and for Windows you need to include #include<windows.h>

hope it helps
Last edited on
Topic archived. No new replies allowed.