title: Why should I use CUDA __shared__ memory as "extern"
body: <p>An example how to use dynamically allocated and therefore extern <strong>shared</strong> memory in CUDA is given: <a href="https://stackoverflow.com/questions/15435559/use-dynamic-shared-memory-allocation-for-two-different-vectors">Use dynamic shared memory allocation for two different vectors</a>   </p>
  
  <pre><code>    extern __shared__ float array[];
      __device__ void func()      // __device__ or __global__ function
      {
          short* array0 = (short*)array; 
          float* array1 = (float*)&amp;array0[128];
          int*   array2 =   (int*)&amp;array1[64];
      }
  </code></pre>
  
  <p>But why should I ever use extern dynamically allocated shared memory when I have to assign it to variables manually anyway?</p>
  
  <p>I do not see any drawback to the following solution:</p>
  
  <pre><code>    __device__ void func()      // __device__ or __global__ function
      {
          __shared__ float array[MAXIMALLY_NEEDED_SIZE];
          short* array0 = (short*)array;
          float* array1 = (float*)&amp;array0[128];
          int*   array2 =   (int*)&amp;array1[64];
      }
  </code></pre>
  
  <p>Clearly, with the first solution, I can save some shared memory. But how would this ever help me?</p>
  
  <p>(I quess there's a good reason for dynamically allocated memory, but I don't see it, so I probably have a lack in my understanding. That's why I ask.)</p>