title: Is it ok if we remove [-Wreturn-local-addr] (warning: address of local variable returned) by using static keyword in c++?
body: <p>I've a function :</p>
  
  <pre><code>int* sumCalc(int *p1, int *p2, int n1, int n2)
  {
      int sum[100] = {0};// = new int[n1+n2];
      for(int i = 0; i &lt; n1+n2; ++i)
      {
          sum[i] = p1[i] + p2[i];
      }
      return sum;
  }
  </code></pre>
  
  <p>I'm calling this function from main() by writing :</p>
  
  <pre><code>int *sum = sumCalc(p1, p2, n1, n2);
  </code></pre>
  
  <p>So here I'm gonna get Warning.</p>
  
  <p>To remove warning I changed my int array <code>sum[100]</code> as static and then I'm returning sum.</p>
  
  <pre><code>int* sumCalc(int *p1, int *p2, int n1, int n2)
  {
      static int sum[100] = {0};// = new int[n1+n2];
      for(int i = 0; i &lt; n1+n2; ++i)
      {
          sum[i] = p1[i] + p2[i];
      }
      return sum;
  }
  </code></pre>
  
  <p>So is it a good practice to make the local variable static?</p>
  
  <p>And if I to take the memory for array <code>sum[100]</code> from the heap then also I think this error would not be there. But can you tell how use new operator here and initialize all elements of array to zero?</p>