title: Is passing a reference to a local variable in main() to other threads/functions bad practice?
body: <p>Consider the below example where I create the local variable <code>specialNumber</code> in <code>main()</code>, and pass it by reference to a new thread, as well as another function (please disregard the lack of a lock/mutex):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  void threadRun(int&amp; number) {
      while(true) {
          std::this_thread::sleep_for(std::chrono::seconds(2));
          std::cout &lt;&lt; number &lt;&lt; std::endl;
          number += 1;
      }
  }
  
  int main() {
      int specialNumber = 5;
      std::thread newThread(threadRun, std::ref(specialNumber));
      otherFunction(specialNumber);
      newThread.join();
  }
  
  
  void otherFunction(int&amp; number) {
      // does something with number
  }
  </code></pre>
  
  <p>I am aware that passing references to local variables around should generally be avoided, as once that function terminates the variable will be out of scope and the reference will be invalid.</p>
  
  <p>However, since the variable is local to <code>main()</code>, and that function won't terminate until the whole program terminates, is there anything wrong with this practice? </p>
  
  <p>My specific use-case would be storing a small object here (mainly consisting of pointers to heap objects along with helper functions), which would be used by multiple threads and/or functions, and passing around a reference to it. I'm aware an alternative is storing it in the heap with a smart pointer such as <code>shared_ptr</code>, but storing such a small object this way seems inefficient to me.</p>
  
  <p>I apologize if any of my terminology is incorrect, I am quite new to C++. Please do correct me!</p>