Cuda malloc gives segmentation fault for 2D matrices

As a part of my code , I am trying to use 2D matrices in CUDA , but it always give me segmentation fault

double** d_matrix;
    cudaMalloc((void***)&d_matrix, m * sizeof(double*));

    for (int i = 0; i < m; i++)
        cudaMalloc((void**)&d_matrix[i], n * sizeof(double));

when I run this using gdb I get

can any one tell me what is the error

cudaMalloc requires a host pointer to initialize. It must be initializing a pointer in host memory.

This is the address of a host memory location:

cudaMalloc((void**)&d_matrix, m * sizeof(double*));

This is the address of a device memory location:

    cudaMalloc((void**)&d_matrix[i], n * sizeof(double));

That is illegal usage of cudaMalloc.

This article covers a variety of topics associated with multi-dimensional array allocation. The case that most closely resembles what you have shown so far is the “general, dynamically allocated 2D case”, but you may wish to study the other methodologies discussed there as well.

please tell me the correct way of declaring the 2D array

Please read the article I linked, both here and on your cross-post on SO.