# Thread: dynamically allocating a 2d array

1. ## dynamically allocating a 2d array

Hi,

what is the syntax for dynamically allocating a 2d array. My 2d array has large dimensions and so have to be dynamically allocated.

Code:
```// I want  double array[5000][5000]

// so I did this, which does not work

double * array[5000];
array = malloc(sizeof(double) * 5000 * 5000)```
How do I do this?
Thanks,

2. yes that work. thanks. I have a different problem now ( I have been working on the problem continuously so sorry, if it seems like I am asking too many questions).

Previously I did as is shown in the link.
Code:
```double **array1 = malloc(5000 * sizeof(double ));
for(i = 0; i < 5000; i++)
array1[i] = malloc(5000 * sizeof(double));```
and I get array1[5000][5000] as desired. Now I have to use array1 in a struct as follows.

Code:
```struct solution { int ell;
double array[5000][5000];
};

// now define 1200 of these structs

struct solution *my_Sol;
// doing the following return a NULL pointer. I am guessing there is no single
// block of memory that big. I'd have to allocate smaller blocks like above
// but the problem is the technique for 2d arrays deals with a single data type

my_Sol = malloc(sizeof(struct solution) * 1200)) //does not work

//nor does the following
int ell;
for (ell = 0; ell < 1200; ell++)
{
my_Sol[ell] =malloc(sizeof(struct solution));
}```
How should I approach the problem?

System Monitor shows that I am using 46% of 1.9GiB right now.

3. 5000*5000*8*1200 = 240000000000
240000000000 / 1073741824 = 223.517

That's the number of GB you are asking for. I don't know what the problem you are working on is, so I don't have any suggestions about how to reduce that. If you can't, you could create one struct at a time then write it to disk, and access them that way (ie, use a database instead of holding everything in memory). That might take a bit of planning, based on what it is you want to do with the values.