C ++ test if the array has been initialized

advertisements

I have a double** mat, and I want to test whether it has already been initialized. If not, I allocate memory space for it. Is this working? :

if(!mat)
{
      mat = new double*[dim1];
      for(int i = 0 ; i < dim1 ; i++)
          mat[i] = new double[dim2];
}

What am I testing with if(! mat) ?

thanks


what that you are checking in:

if (!mat)

is if the location mat is pointing to is NULL (0).

this well work just if the default for pointers is zero, of if you have declared your pointer like this:

double ** mat = NULL;