# Using shared memory dynamically

**URL:** <https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949>\
**Category:** CUDA Programming and Performance\
**Created:** [December 10, 2014, 5:22pm UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949 "2014-12-10T17:22:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![titan1432](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@titan1432](https://forums.developer.nvidia.com/u/titan1432)\
**Post date:** [December 10, 2014, 5:22pm UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949/1 "2014-12-10T17:22:12Z")

</div>

Hi all, I am very new in CUDA. I want to use Shared Memory and the array I want to add to share memory has a size “int a” (not constant, can change from 1 initial values to another). So my question is: can I allocate shared memory dynamically, like in c++ with the help of new function? Should I delete it at the end of my _global_ function?  
Thanks

---

<div class="post-metadata">

**Author:** ![titan1432](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@titan1432](https://forums.developer.nvidia.com/u/titan1432)\
**Post date:** [December 10, 2014, 5:26pm UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949/2 "2014-12-10T17:26:43Z")

</div>

Can I use std::vector?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [December 10, 2014, 6:13pm UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949/3 "2014-12-10T18:13:56Z")

</div>

Yes, you can allocate shared memory dynamically. You do it by using the “extern” keyword while not specifying an array size on the **shared** declaration in your kernel code:

```auto
extern __shared__ int my_shared_data[];

```

and then specify the size dynamically at kernel launch time, by passing the desired size _in bytes_ of the shared memory array as the 3rd kernel configuration parameter:

```auto
my_kernel<<<gridSize, blockSize, sharedSize>>>(...);

```

You may also want to read the programming guide:  
[http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared)

and there are many examples of this as well:  
[http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared-memory-variable-declarations](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared-memory-variable-declarations)

std::vector is not usable in CUDA device code.

---

<div class="post-metadata">

**Author:** ![titan1432](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@titan1432](https://forums.developer.nvidia.com/u/titan1432)\
**Post date:** [December 10, 2014, 6:48pm UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949/4 "2014-12-10T18:48:43Z")

</div>

Thanks for the reply! So I cannot use std::vector anywhere within device kernel?  
That is sad-((  
Also can I use my own structure in device kernel? Something like:

```auto
struct Pixel{
      float p;
      }

```

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [December 10, 2014, 7:39pm UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949/5 "2014-12-10T19:39:46Z")

</div>

Yes, you can use your structure. CUDA supports most aspects of C and C++, including objects. Most items from the standard template library are not usable in device code.

---

<div class="post-metadata">

**Author:** ![titan1432](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@titan1432](https://forums.developer.nvidia.com/u/titan1432)\
**Post date:** [December 11, 2014, 12:45am UTC](https://forums.developer.nvidia.com/t/using-shared-memory-dynamically/35949/6 "2014-12-11T00:45:52Z")

</div>

Thanks
