pointer to pointer

Hi, i've posted a message before but i didn't manage to allocate and free a pointer to pointer variable (matrix). I want to use it as Matrix[i][j] to manage better.

The first code I tried is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double **pMatrix=new double*[N];
  for(i=0;i<=N;i++)
  {
     pMatrix[i]=new double[N];
     //I want a NxN square matrix
  }
  //now, are all elements 0? Do I have to inizialize them?
/////////////////////////////////
//I try to free memory
  for(i=0;i<=N;i++)
  {
     delete []pMatrix[i];
     //I suppose the error is in this line
  }
delete []pMatrix;


I have also tried allocating with calloc.. let me show you the code
1
2
3
4
5
6
7
8
9
10
11
12
//allocate
double **pMatrix = (double **)calloc(N,sizeof(*double));
for(i=0;i<=N;i++)
  {
     *pMatrix[i]=(double*)calloc(N,sizeof(double));
  }
//free
for(i=0;i<=N;i++)
  {
     free(pMatrix[i]);
  }
free(pMatrix);


Can anybody help? if it is possible, please, would you write me the code correctly?
Ah, I use visual c++ 2008, but i don't think is a compiler's problem.

Thank you so much!
What are you trying to do? Also, see http://cplusplus.com/forum/articles/17108/ for why you may want to reconsider using multidimensional arrays.
I managed to solve my problem finally. Thanks for these tutorials guys!!
Topic archived. No new replies allowed.