how to create a dynamic array in the device function?

I have a C++ code like this:

void test(int n)

{

  double *da=new double[n];

  ...

  delete []da;

}

And if I want to execute the test() function in the device, like:

__device__ void test(int n)

{

  double *da=new double[n];

  ...

  delete []da;

}

This is not working as expected, so I try another way, that also does not work:

void test(int n)

{

  double * da;

  cudaMalloc((void**)&da,n*sizeof(double));

...

  cudaFree(da);

}

So how to solve this problem?

There is no dynamic memory allocation permitted inside CUDA kernels.

As avidday said - no dynamic arrays. You can do dynamic shared memory allocation at kernel invocation. Failing that, just pass in a pointer to a global memory space the size you want.

As avidday said - no dynamic arrays. You can do dynamic shared memory allocation at kernel invocation. Failing that, just pass in a pointer to a global memory space the maximum size you want.

Thanks.