title: Correct way of passing pointer to another thread
body: <p>I've got 2 functions, <code>func1()</code> and <code>func2()</code>. <code>func2</code> takes a character array as input. Both functions run on different threads. I call <code>func2</code> from <code>func1</code>. When I passed a stack allocated array to <code>func2</code>, I got garbage values when I printed the array from inside <code>func2()</code>. However, when I passed a heap allocated array to <code>func2</code>, I got the correct string inside <code>func2()</code> i.e.</p>
  
  <pre><code>func2(char * char_array)
  {
  /* Some code */
  cout&lt;&lt;char_array;
  }
  </code></pre>
  
  <p>/* <strong>This does not work(Garbage values were printed in func2())</strong>  */</p>
  
  <pre><code>func1()
  {
  char array_of_char[SIZE];
  memset(array_of_char,0,SIZE);
  strncpy(array_of_char,"SOME_STRING",SIZE);
  func2(array_of_char); //Asynchronous call to func2(). func1() proceeds immediately.
  /*
  some code
  */
  }
  </code></pre>
  
  <p>/* <strong>This works(Correct values were printed in func2)</strong> */</p>
  
  <pre><code>func1()
  {
  char * array_of_char=new char[SIZE];
  memset(array_of_char,0,SIZE);
  strncpy(array_of_char,"SOME_STRING",SIZE);
  func2(array_of_char); //Asynchronous call to func2(). func1() proceeds immediately.
  /*
  some code
  */
  }
  </code></pre>
  
  <p>Does this mean that in multi-threaded programs, whenever some pointer has to be passed between different threads, the pointer should always be pointing at a heap-allocated memory?</p>
  
  <blockquote>
    <p>Please note that func2() is actually a callback function which
    executes on the occurrence of an event. I've hidden those details in
    the question. Func1() does not stop/wait for the execution of func2().</p>
  </blockquote>
  
  <p><strong>Edit</strong>: I feel like I need to provide some more details of my implementation. In my program, I'm using Datastax C++ client library for Cassandra. Please find the link in the end, containing some details of the functions used in the program:</p>
  
  <pre><code>int main()
  {
  
  func1();
  /* some code */
  return 0;
  }
  </code></pre>
  
  <p>/* <strong>This does not work(Garbage is printed in func2)</strong> */</p>
  
  <pre><code>void func1()
  {
  /* some code */
  char array_of_char[SIZE];
  memset(array_of_char,0,SIZE);
  strncpy(array_of_char,"SOME_STRING",SIZE);
  CassFuture* l_query_future = NULL;
  /* some code where query is created */
  l_query_future = cass_session_execute(rtGetSession(), l_stmt); //l_stmt is the query statement, rtgetSession() returns CassSession *
  cass_future_set_callback ( l_query_future, func2, (void *)array_of_char); //details in the link given in the end
  /* some code */
  }
  </code></pre>
  
  <p>/* <strong>This works(Correct values were printed in func2)</strong> */</p>
  
  <pre><code>void func1()
  {
  /* some code */
  char * array_of_char=new char[SIZE];
  memset(array_of_char,0,SIZE);
  strncpy(array_of_char,"SOME_STRING",SIZE);
  CassFuture* l_query_future = NULL;
  /* some code where query is created */
  l_query_future = cass_session_execute(rtGetSession(), l_stmt); //l_stmt is the query statement, rtgetSession() returns CassSession *
  cass_future_set_callback ( l_query_future, func2, (void *)array_of_char);
  /*
  some code
  */
  }
  
  
  void func2(CassFuture* l_query_future, void * data)
  {
  /* some code */
  cout&lt;&lt;(char *)data;
  }
  </code></pre>
  
  <p>References for Datastax driver APIs:</p>
  
  <ol>
  <li><a href="https://docs.datastax.com/en/developer/cpp-driver/2.7/api/struct.CassFuture/#function-cass_future_set_callback" rel="nofollow noreferrer">cass_future_set_callback</a></li>
  <li><a href="https://docs.datastax.com/en/developer/cpp-driver/2.7/api/struct.CassFuture/" rel="nofollow noreferrer">CassFuture</a></li>
  <li><a href="https://docs.datastax.com/en/developer/cpp-driver/2.7/api/struct.CassSession/" rel="nofollow noreferrer">CassSession</a></li>
  <li><a href="https://docs.datastax.com/en/developer/cpp-driver/2.7/api/struct.CassSession/" rel="nofollow noreferrer">cass_session_execute</a></li>
  </ol>