# Thread: 2D array in dynamic memory

1. ## 2D array in dynamic memory

Hello,

I am attempting to create and access a 2d array in dynamic memory. I can easily create the array in stack memory with the following code:

Code:
`double d[5][5];`
My question is how to do this with dynamic memory? I have an integer variable entitled vertNum to represent the number of vertices.

How do I create an array in dynamic memory using the vertNum variable?

So far I have the code below. How do I set the column of each and access the column?

Code:
```double ** row = new double * [vertNum];
double * col = new double [vertNum];
*row = col;```

2. Code:
```double **d = new double*[5];

for( int i = 0; i < 5; i++ ) {

d[i] = new double[5];

}```
And to delete the memory used...

Code:
```for( int j = 0; j < 5; j++ ) {

delete[] d[j];

}

delete[] d;```

3. Thanks,

One more thing.

How would I pass the 2D array to a function?

For instance, if I have a function called foo, how would it get passed and received by another function foo?

Like the following:

Code:
```//call the function
foo(twoDimArray);```
Code:
```//call the function
void foo(double **twoDimArray);```