# how to create a dynamic array in the device function?

**URL:** <https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242>\
**Category:** CUDA Programming and Performance\
**Created:** [November 13, 2009, 9:54am UTC](https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242 "2009-11-13T09:54:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wlx](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wlx](https://forums.developer.nvidia.com/u/wlx)\
**Post date:** [November 13, 2009, 9:54am UTC](https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242/1 "2009-11-13T09:54:23Z")

</div>

I have a C++ code like this:

```auto
void test(int n)

{

  double *da=new double[n];

  ...

  delete []da;

}

```

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

```auto
__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:

```auto
void test(int n)

{

  double * da;

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

...

  cudaFree(da);

}

```

So how to solve this problem?

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [November 13, 2009, 9:55am UTC](https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242/2 "2009-11-13T09:55:51Z")

</div>

There is no dynamic memory allocation permitted inside CUDA kernels.

---

<div class="post-metadata">

**Author:** ![Tigga](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Tigga](https://forums.developer.nvidia.com/u/Tigga)\
**Post date:** [November 13, 2009, 11:01am UTC](https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242/3 "2009-11-13T11:01:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tigga](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Tigga](https://forums.developer.nvidia.com/u/Tigga)\
**Post date:** [November 13, 2009, 11:01am UTC](https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242/4 "2009-11-13T11:01:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wlx](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wlx](https://forums.developer.nvidia.com/u/wlx)\
**Post date:** [November 13, 2009, 2:22pm UTC](https://forums.developer.nvidia.com/t/how-to-create-a-dynamic-array-in-the-device-function/13242/5 "2009-11-13T14:22:36Z")

</div>

Thanks.
