# Extern pointer shared memory

**URL:** <https://forums.developer.nvidia.com/t/extern-pointer-shared-memory/41518>\
**Category:** CUDA Programming and Performance\
**Created:** [February 20, 2016, 3:44pm UTC](https://forums.developer.nvidia.com/t/extern-pointer-shared-memory/41518 "2016-02-20T15:44:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![realbas89](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@realbas89](https://forums.developer.nvidia.com/u/realbas89)\
**Post date:** [February 20, 2016, 3:44pm UTC](https://forums.developer.nvidia.com/t/extern-pointer-shared-memory/41518/1 "2016-02-20T15:44:19Z")

</div>

I’m trying use pointer to a struct in shared memory but I don’t know how to setting memory size for each block since their size differ between blocks, something like:

```auto
struct reg{
   int *val;
   int sizeval;
};

__shared__ reg data;

__global__ void kernel1(){
   //initialize data for each block
}

__global__ void kernel2(){
   //use same data pointer here but when I try use *val gets memory error
}

int main(){
   kernel1<<< nblocks, nthreads >>>(); 
   kernel2<<< nblocks, nthreads >>>(); //here I need the size of shared memory to data
                                       //but how can pass size per block?
}

```

There is a way to pass different size to shared memory or allocate data as static in _kernel1()_ and use same variable in _kernel2()_?

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [February 20, 2016, 4:21pm UTC](https://forums.developer.nvidia.com/t/extern-pointer-shared-memory/41518/2 "2016-02-20T16:21:54Z")

</div>

Shared memory is not persistent between kernels (not even between blocks), so you can’t do this with shared memory.  
Use normal device memory instead, or merge the two kernels into a single one.

---

<div class="post-metadata">

**Author:** ![realbas89](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@realbas89](https://forums.developer.nvidia.com/u/realbas89)\
**Post date:** [February 20, 2016, 4:37pm UTC](https://forums.developer.nvidia.com/t/extern-pointer-shared-memory/41518/3 "2016-02-20T16:37:52Z")

</div>

If is not persistent then why have extern shared memory (Programming guide B.2.3)?

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [February 20, 2016, 7:32pm UTC](https://forums.developer.nvidia.com/t/extern-pointer-shared-memory/41518/4 "2016-02-20T19:32:59Z")

</div>

Read the documentation you cited and you’ll find that “extern” on shared memory variables does something completely different than you expect - it defers the decision over their size until runtime.

Admittedly that is abusing this keyword that would otherwise have no sensible meaning in the context of shared memory.  
Have you read introductory section 2 about the CUDA programming model?
