title: Pointer to local variable is stored outside the scope of this variable
body: <p>For the following code snippet, which is a part of <a href="https://github.com/p-alik/gearmand/blob/master/libgearman/worker.cc#L668-L672" rel="nofollow noreferrer">libgearman</a></p>
  
  <pre><code>gearman_job_st *gearman_worker_grab_job(gearman_worker_st *worker_shell,
                                          gearman_job_st *job,
                                          gearman_return_t *ret_ptr)
  {
      if (worker_shell and worker_shell-&gt;impl())
      {
  
          ...
  
          gearman_return_t unused;
          if (ret_ptr == NULL)
          {
              ret_ptr= &amp;unused;
          }
          ...
      }
  
      assert(*ret_ptr != GEARMAN_MAX_RETURN);
      return NULL;
  }
  </code></pre>
  
  <p><a href="https://www.viva64.com/en/w/V507/" rel="nofollow noreferrer">PVS-Studio</a> reported:</p>
  
  <pre><code>Viva64-EM
  full
  671
  /nfs/home/xxx/src/gearmand/libgearman/worker.cc
  error
  V506
  Pointer to local variable 'unused' is stored outside the scope of this variable. Such a pointer will become invalid.
  false
  2
      {
        ret_ptr= &amp;unused;
      }
  ------------
  </code></pre>
  
  <p>Regarding the question <a href="https://stackoverflow.com/a/18473875/2789312">Pointer to local variable outside the scope of its declaration</a>, if I understand that correctly, <code>malloc</code> and <code>free</code> should be used for refactoring. My question is if there is an other appropriate refactoring alternative. For instance using of <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr" rel="nofollow noreferrer">std::unique_ptr</a>:</p>
  
  <p><code>ret_ptr = std::make_unique&lt;gearman_return_t&gt;();</code></p>